String as method call?

Should be simple but I can't eval a string as a method call. I have
poked around online a fair amount to get to the bottom of this mundane
task, with no results.

I have a hash "fields" that contains a series of AR field names and an
object called "news" which is an AR object with that has the field
names that are coming in from the hash. I just want to assign the hash
values to the same field value from the news object.

I have tried all these variations but with no luck. Mostly just blows
up with syntax errors.

fields.each_pair {|key, value| news.eval("key") = value }

fields.each_pair {|key, value| news."#{key}" = value }

fields.each_pair {|key, value| "#{news.key}" = value }

fields.each_pair {|key, value| "news.#{key} = #{value}" }

fields.each_pair {|key, value| news.send key = value }

How do I do this?

Thanks, Elliott


but the syntax you were struggling to find is probably:

fields.each_pair {|key,value| news.send("#{key}=", value) }


Rob Biedenharn

Thanks Rob.

The send syntax worked perfectly! In this case I don't think
news.update_attributes(fields) will work, because fields hash values
conflicts with my validations, but It helped me to think a bit
differently in general.