class Frank def initialize(name) puts "Hi #{name}." end def kill puts "I'm dead!" end end def do_it name f = Frank.new "Equinox" f.kill puts " -- #{name}" end do_it (rabid = "Forethought")
Now, we're tasked with converting this into bytecode for RubyEx, keeping consideration of the entire object model. How do we handle the creation of classes, for instance? Or defining functions? We need to be aware of our “context”, which is this strange main Object. MRI uses an internal variable, ruby_class (name changed) to keep track of this. It's important.
ruby-talk:207801 and ruby-talk:173746, both care of Guy Decoux, help us here.
At the top level, ruby_class is Object. methods(false) returns only methods defined on that object, not ones inherited or otherwise from elsewhere. Note that the main object has very few singleton methods - any top-level defined functions do not go into main; they go into Object. Check out Object.instance_methods(false). That's where the top-level functions go, and note the same for Kernel doesn't list them. Of course main is a sort of Object, and thus those methods are available here, and everywhere else.
Be careful about using irb to survey this landscape - it pollutes the namespace considerably.
Also note that ruby_cbase is used for resolving constants, not method defs; ruby_class is used for method definitions (as opposed to a previous misconception by myself). Credit again to Guy for pointing this out.