From: ts Date: 2008-04-28T20:03:18+09:00 Subject: Re: undefined local variable Johan Eriksson wrote: > the_class.class_eval <<-EOA First, you don't need the evil eval > > def self.load_from_file(filename) > yaml_dict = YAML::load(File.open(filename)) > object_list = yaml_dict[yaml_dict.keys[0]].map do |from_file| > puts "from_file: #{from_file}" > new_obj = self.class.new self will be `the_class', this mean that you must write new_obj = new > from_file.map do |obj| > puts "obj: #{obj}" > obj_as_list = obj.to_a > puts "obj_as_list: #{obj_as_list}" > if obj_as_list[1].is_a? String > obj_as_list[1] = "'#{obj_as_list[1]}'" > end > str = "new_obj.#{obj_as_list[0]} = #{obj_as_list[1]}" Another occurence of the evil eval > eval(str) > end > new_obj > end > object_list > end > EOA Try something like this def the_class.load_from_file(filename) yaml_dict = YAML::load(File.open(filename)) object_list = yaml_dict[yaml_dict.keys[0]].map do |from_file| puts "from_file: #{from_file}" new_obj = new from_file.map do |obj| puts "obj: #{obj}" obj_as_list = obj.to_a puts "obj_as_list: #{obj_as_list}" new_obj.send("#{obj_as_list[0]}=", obj_as_list[1]) end new_obj end object_list end Guy Decoux