From: "David A. Black" Date: 2008-04-26T03:25:06+09:00 Subject: Re: undefined local variable Hi -- On Sat, 26 Apr 2008, Johan Eriksson wrote: > Chris Hulan wrote: >> On Apr 25, 11:16 am, Johan Eriksson wrote: >>> Hi! >>> >>> I have some troubles with this. >>> >>> obj_as_list.each do |att| >>> if att[1].is_a?(String) >>> att[1] = "'#{att[1]'" >> You're missing a '}' here ----------^ >> >> does that help? >> > > Nope. I am providing the surrounding function here. (It is defined inside a > class.) > > def load_from_file(filename) > require 'yaml' > yaml_dict = YAML::load(File.open(filename)) > object_list = [] #The list that should be returned > yaml_dict[yaml_dict.keys[0]].each do |yaml_obj| > new_obj = self.class.new > yaml_obj.each do |obj| > obj_as_list = obj.to_a > obj_as_list.each do |att| > if att[1].is_a?(String) > att[1] = "'#{att[1]}'" > end > str = "new_obj.#{att[0]} = #{att[1]}" > eval(str) > end > end > object_list << new_obj > end > object_list > end > > And the error: > > NameError: undefined local variable or method `att' for main:Object If att[1] is, say, :att, then "#{att[1]}" is "att" and you've got a dangling att. The whole eval thing seems extremely fragile (which eval things usually are). I imagine there's a more robust and probably shorter way to do this, but I'm not sure what the data coming in and out are supposed to be like. David -- Rails training from David A. Black and Ruby Power and Light: INTRO TO RAILS June 9-12 Berlin ADVANCING WITH RAILS June 16-19 Berlin INTRO TO RAILS June 24-27 London (Skills Matter) See http://www.rubypal.com for details and updates!