Created
September 10, 2021 05:19
-
-
Save JoshCheek/55241aa749b44288d1c962b924765b2a to your computer and use it in GitHub Desktop.
The Devil's Typecasting in Ruby!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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