====== Bytecode Design ====== 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 [[:start|RubyEx]], keeping consideration of the entire [[dev:objectmodel|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. [[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/207801|ruby-talk:207801]] and [[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/173746|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. * **obj.instance_eval** renders: **self** = **obj**, **ruby_class** = **obj singleton class** - methods defined on instances of **obj**'s singleton class (**obj** itself) * **obj.class_eval** renders: **self** = **obj**, **ruby_class** = **obj** - methods defined on instances of **obj** * top level renders: **self** = **main**, **ruby_class** = **Object**; //note this is **Object** and not **Kernel**// - methods defined on instances of __**Object**__ * **class A** renders: **self** = **ruby_class** = **A** - methods defined on instances of **A** * **A#a** renders: self = **A** instance, **ruby_class** = **A** - methods defined on instances of **A** * **A = Class.new** renders: **self** = **ruby_class** = **A** - methods defined on instances of **A** 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.