Skip to content

Instantly share code, notes, and snippets.

@judofyr
Created March 29, 2012 15:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save judofyr/2238438 to your computer and use it in GitHub Desktop.
Save judofyr/2238438 to your computer and use it in GitHub Desktop.
Experimenting with MRI internals in FFI
require 'ffi'
class Object
def memory_location
object_id * 2
end
def to_pointer
FFI::Pointer.new(memory_location)
end
def class=(klass)
to_pointer.put_ulong(8, klass.memory_location)
end
def unfreeze
to_pointer.tap do |p|
p.put_ulong(0, p.get_ulong(0) &~ (1<<11))
end
self
end
end
if $0 == __FILE__
a = Object.new
a.class = Array
p a.class
a.freeze
p a.frozen?
a.unfreeze
p a.frozen?
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment