From: "Mauricio Fernández" Date: 2003-02-20T01:59:02+09:00 Subject: Re: Sourcing files On Thu, Feb 20, 2003 at 12:41:03AM +0900, Brian Candler wrote: > You'd think there would be a more Rubyesque way of doing this. I thought > about an object which represents your configuration information: > > # main.rb > class Config > attr_accessor :x,:y,:z > end > conf = Config.new > conf.instance_eval { load 'config.rb' } # no > puts conf.x > > # config.rb > self.x = 23 > > But 'load' doesn't work in that way inside instance_eval, since it seems to > reset the context to the object 'main' > 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 -- _ _ | |__ __ _| |_ ___ _ __ ___ __ _ _ __ | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ | |_) | (_| | |_\__ \ | | | | | (_| | | | | |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_| Running Debian GNU/Linux Sid (unstable) batsman dot geo at yahoo dot com Debian is like Suse with yast turned off, just better. :) -- Goswin Brederlow