From: Robert Klemme Date: 2009-12-08T23:31:08+09:00 Subject: Re: convert string into a variable object 2009/12/8 David A. Black : > HI -- > > On Tue, 8 Dec 2009, Steve Wilhelm wrote: > >> Toon Willems wrote: >>> >>> On 07 Dec 2009, at 21:35, Ad Ad wrote: >>> >>>>> to do. >>>> >>>> doesnt require the  creation of any sort of a container variable. >>>> -- >>>> Posted via http://www.ruby-forum.com/. >>>> >>> >>> A much better way to do this would be to store the city names in a hash. >>> your code would then be: >>> >>> File.open(@file). each do |x| >>>  @hash[x] = true >>> end >> >> Would it not be better to convert to symbols? That is: >> >> File.open(@file). each do |x| >>  @hash[x.strip.to_sym] = true >> end >> >> if @hash[:toronto] >> or >> if @hash[:"palo alto"] >> >> or >> >> if @hash[city.to_sym] > > In addition to the garbage collection issue that David M. pointed out, > symbols seem an odd choice to me for city names. I always think of > symbols as appropriate for label-like functionality and values taken > from a limited set. Strings are a better choice for general > representation of text. I would certainly use String for city names. Hard encoding city names in classes which would be regenerated over and over again is not a good solution. Basically, you want to stick all city names as keys in a Hash and work from there. The mere fact that Ruby is able to generate local, instance or global variables at runtime does not mean it is a proper means in all cases. The use case at hand does not call for meta programming - it just needs a data structure which can work with appropriate key values. A Hash seems like a good fit here. My 0.02EUR. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/