Created
January 24, 2023 11:47
-
-
Save KJTsanaktsidis/80a7005cd0784f73b83ab121b1b82cc9 to your computer and use it in GitHub Desktop.
Renaming an anonymous class 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
kj@kj-thinkpad ~ % cat rename.rb | |
#!/usr/bin/env ruby | |
require 'fiddle' | |
libruby = Fiddle::Handle.new | |
rb_set_class_path_string = Fiddle::Function.new( | |
libruby['rb_set_class_path_string'], | |
[ | |
Fiddle::TYPE_UINTPTR_T, | |
Fiddle::TYPE_UINTPTR_T, | |
Fiddle::TYPE_UINTPTR_T | |
], | |
Fiddle::TYPE_VOID | |
) | |
GC.disable | |
def object_addr(o) | |
istr = Object.instance_method(:inspect).bind(o).call | |
istr =~ /:(0x[0-9A-Fa-f]+)>/ | |
$1.to_i(16) | |
end | |
cl = Class.new | |
puts "class => #{cl}" | |
name = "Foo::BarKlass" | |
rb_set_class_path_string.call(object_addr(cl), object_addr(Object), object_addr(name)) | |
puts "class => #{cl}" | |
kj@kj-thinkpad ~ % /opt/ruby33/bin/ruby rename.rb | |
class => #<Class:0x00007f1e3fcd1360> | |
class => Foo::BarKlass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment