From: 7stud -- Date: 2007-11-13T14:22:45+09:00 Subject: Re: name of a variable, at runtime Colin Summers wrote: > Well, technically it will be a Rails object, so I don't create it, > ActiveRecord does, but I bet the send(part) thing will work. Thanks. > This will make a really LOOOOONG table get generated with a few lines > of code instead. > > --Colin In case you wanted an explanation of how things work, take a look at this example: class Human def knee=(str) @knee = str end def knee return @knee end end fred = Human.new fred.knee = 'plastic' puts fred.knee --->plastic When you write: fred.knee = 'plastic' you are actually calling a method--the method named 'knee='. In Ruby, method names can have characters like '=' and '?' in them. When the method 'knee=' is called, the statement in the body of the method is executed, and that statement assigns a value to the member variable @knee. Likewise, when you write: puts fred.knee that calls the method named 'knee', and the knee method is defined to return the value of the member variable @knee. Therefore, the problem you were faced with becomes: how do you call a method if you have the name of the method as a string. That's where Object#send comes in--it allows you to do just that. Although it wouldn't make much sense, you could actually define the knee= method like this: class Human def knee=(str) @head = str end def head return @head end end fred = Human.new fred.knee = 'steel' puts fred.head --->steel -- Posted via http://www.ruby-forum.com/.