Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created September 10, 2021 05:19
Show Gist options
  • Save JoshCheek/55241aa749b44288d1c962b924765b2a to your computer and use it in GitHub Desktop.
Save JoshCheek/55241aa749b44288d1c962b924765b2a to your computer and use it in GitHub Desktop.
The Devil's Typecasting in Ruby!
require 'fiddle'
public def to_ptr
Fiddle::Pointer.new Fiddle.dlwrap self
end
public def to!(klass)
to_ptr[8, 8] = klass.to_ptr.ref[0, 8]
end
A, B = Class.new, Class.new
a = A.new
a # => #<A:0x000000012a9557b0>
a.to! B
a # => #<B:0x000000012a9557b0>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment