From: Robert Klemme Date: 2010-12-05T21:50:21+09:00 Subject: Re: Better way to read property than using eval()? On 05.12.2010 00:27, Matt Slay wrote: > Is there a better way to read a property from a model if you have a > string that contains the property name that you want to read? > > Here is what I am doing now: > > eval('@tolerance2.' + @size[2]) > > I create the @tolerance2 object by using a .find() in Rails, and I have > the string value which the name of the property I need off of the model. > > I'm thinking there is a better Ruby way to do this. How about: x = @tolerance2.send(@size[2]) At least this is safer than using eval. Just think what would happen if @size[2] = 'to_s and system("rm -rf /")' :-) Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/