DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Daniel has posted 5 posts at DZone. View Full User Profile

Getting An Array Of Strings From A Char** With FFI And Ruby

06.07.2010
| 2373 views |
  • submit to reddit
        This code adds a method to the FFI::Pointer class that makes it easy to unravel an array of null terminated strings from a char** type.

require 'ffi'

class FFI::Pointer
  def read_array_of_string
    elements = []

    loc = self

    until ((element = loc.read_pointer).null?)
     elements << element.read_string
     loc += FFI::Type::POINTER.size
    end

    elements
  end
end