Leah Cunningham
2006-Mar-16 17:20 UTC
[Rails] Passing function names from string variables
This may be a more generic Ruby question, so I''m going to ask on the Ruby forum, but I''m trying to figure out if there is a way to pass in the string value of a variable as the name of a method. For example, I would like to do something like: def sort_obj_by_uid(objects,@attr) @tmparray = Array.new @tmphash = Hash.new for object in @objects if ! @tmphash.has_key?(object.@attr.to_s) @tmphash[object.@attr.to_s] = Array.new end @tmphash[object.@attr.to_s].push(object) end end Where the method name is the @attr value. -- Leah Cunningham : d416-585-9971x692 : d416-703-5977 : m416-559-6511 Frauerpower! Co. : www.frauerpower.com : Toronto, ON Canada
dblack@wobblini.net
2006-Mar-16 17:23 UTC
[Rails] Passing function names from string variables
Hi -- On Thu, 16 Mar 2006, Leah Cunningham wrote:> This may be a more generic Ruby question, so I''m going to ask on the Ruby > forum, but I''m trying to figure out if there is a way to pass in the string > value of a variable as the name of a method. > > For example, I would like to do something like: > > def sort_obj_by_uid(objects,@attr) > @tmparray = Array.new > @tmphash = Hash.new > for object in @objects > if ! @tmphash.has_key?(object.@attr.to_s) > @tmphash[object.@attr.to_s] = Array.new > end > @tmphash[object.@attr.to_s].push(object) > end > end > > Where the method name is the @attr value.Yes: send, like this: object.send(@attr).to_s If you want to send additional arguments, you put them in the second, third... positions. David -- David A. Black (dblack@wobblini.net) Ruby Power and Light, LLC (http://www.rubypowerandlight.com) "Ruby for Rails" chapters now available from Manning Early Access Program! http://www.manning.com/books/black
Very easy: @obj.send(@attr.to_sym) On 3/16/06, Leah Cunningham <leah@frauerpower.com> wrote:> This may be a more generic Ruby question, so I''m going to ask on the Ruby > forum, but I''m trying to figure out if there is a way to pass in the string > value of a variable as the name of a method. > > For example, I would like to do something like: > > def sort_obj_by_uid(objects,@attr) > @tmparray = Array.new > @tmphash = Hash.new > for object in @objects > if ! @tmphash.has_key?(object.@attr.to_s) > @tmphash[object.@attr.to_s] = Array.new > end > @tmphash[object.@attr.to_s].push(object) > end > end > > Where the method name is the @attr value. > > -- > Leah Cunningham : d416-585-9971x692 : d416-703-5977 : m416-559-6511 > Frauerpower! Co. : www.frauerpower.com : Toronto, ON Canada > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
dblack@wobblini.net
2006-Mar-16 17:25 UTC
[Rails] Passing function names from string variables
On Thu, 16 Mar 2006, dblack@wobblini.net wrote:> Hi -- > > On Thu, 16 Mar 2006, Leah Cunningham wrote: > >> This may be a more generic Ruby question, so I''m going to ask on the Ruby >> forum, but I''m trying to figure out if there is a way to pass in the string >> value of a variable as the name of a method. >> >> For example, I would like to do something like: >> >> def sort_obj_by_uid(objects,@attr) >> @tmparray = Array.new >> @tmphash = Hash.new >> for object in @objects >> if ! @tmphash.has_key?(object.@attr.to_s) >> @tmphash[object.@attr.to_s] = Array.new >> end >> @tmphash[object.@attr.to_s].push(object) >> end >> end >> >> Where the method name is the @attr value. > > Yes: send, like this: > > object.send(@attr).to_sWhoops, I meant: object.send(@attr.to_s) David -- David A. Black (dblack@wobblini.net) Ruby Power and Light, LLC (http://www.rubypowerandlight.com) "Ruby for Rails" chapters now available from Manning Early Access Program! http://www.manning.com/books/black