From: Brian Candler Date: 2003-02-20T06:19:29+09:00 Subject: Re: Sourcing files On Thu, Feb 20, 2003 at 01:59:02AM +0900, Mauricio Fern?ndez wrote: > Using instance_eval is a good idea IMHO: > > batsman@tux-chan:/tmp$ expand -t 2 h.rb > class Config > attr_accessor :x, :y, :z > end > > USEFILE = false > > conf = Config.new > conftext = case USEFILE > when true > File.open("./config.rb") { |f| f.read } > when false > 'self.x = 1; self.y = 2; self.z = "test" ' > end > conf.instance_eval conftext > conf.instance_variables.each do |f| > puts "#{f} => #{conf.instance_eval(f).inspect} " > end > batsman@tux-chan:/tmp$ ruby h.rb > @z => "test" > @y => 2 > @x => 1 That's tidier. The config file can say '@x=1' instead of 'self.x=1' which is cleaner too. I guess it's an example of an application which would be cleaner without the local variable / method dichotomy; ATM there's no way to invoke an attribute writer without prefixing it with the object reference. B.