Skip to content

Instantly share code, notes, and snippets.

@ahmadsherif
Last active August 29, 2015 13:56
Show Gist options
  • Save ahmadsherif/e1a3a47dead2646134ac to your computer and use it in GitHub Desktop.
Save ahmadsherif/e1a3a47dead2646134ac to your computer and use it in GitHub Desktop.
# In response to this question http://stackoverflow.com/questions/21899007/get-enumerator-method
require 'dl'
enum = [1,2,3].reject
enum_ptr = DL::CPtr.to_ptr(enum.object_id << 1)
rdata_struct = enum_ptr[0, DL::SIZEOF_LONG * 5].unpack('Q5')
enum_struct_ptr = DL::CPtr.to_ptr(rdata_struct.last)
enum_struct = enum_struct_ptr[0, DL::SIZEOF_LONG * 8].unpack('Q8')
libruby = DL.dlopen('libruby.so')
id2str_cfunc = DL::CFunc.new(libruby['rb_id2str'], DL::TYPE_VOIDP, 'rb_id2str')
str_ptr = id2str_cfunc.call([enum_struct[1]])
puts DL::CPtr.to_ptr(str_ptr).to_value # => reject
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment