code block… » …which it then executes in the scope of the receiver. » Methods will become class methods (because self == the class) obj.instance_eval do self # => obj @v # => obj's instance var end
» creates instance methods » can be used to do stuff inside the class definition [String, Array, Hash].each do |cls| cls.class_eval { include HelloWorld } end
not found in the inheritance chain. » By default throws a NoMethodError. » Can be modified to catch any method call, thus “creating” a ghost method » The name of which you don't have to even know.
"I might as well say something: #{args[0]}" else super end end end gary = Speaker.new gary.talk("Destroy it") # => NoMethodError gary.speak("Just destroy it!") # => "I might as well say something: Just destroy it!"
do |meth| # getter define_method(meth) do instance_variable_get("@#{meth}") end # setter define_method("#{meth}=") do |wut| instance_variable_set("@#{meth}", wut) end end end end