From: Brian Candler Date: 2010-02-16T18:11:38+09:00 Subject: Re: 'def', but with a closure Brian Candler wrote: > "The current class" is set inside a class ... end construct. But as > you've found, this starts a new scope. So you can use class_eval > instead, which sets both the current object and the current class. Note that instance_eval sets the current class to the object's singleton class, whereas class_eval sets the current class to the object itself, if that object is a Class. So this works: @cars.each do |owner, model| widget = TkLabel.new(:text => model) widget.instance_eval do def get_owner "nobody" end end end Unfortunately, define_method is a method of Module, not Object. So this doesn't work: @cars.each do |owner, model| widget = TkLabel.new(:text => model) widget.instance_eval do define_method(:get_owner) do owner end end end Hence the need for (class << widget; self; end).class_eval Regards, Brian. -- Posted via http://www.ruby-forum.com/.