Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Evaluating the string in a variable to use as a method name

Reply
Thread Tools

Evaluating the string in a variable to use as a method name

 
 
heinous (sent by Nabble.com)
Guest
Posts: n/a
 
      03-16-2006

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.

--
View this message in context: http://www.nabble.com/Evaluating-the....html#a3440567
Sent from the ruby-talk forum at Nabble.com.



 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      03-16-2006

"heinous (sent by Nabble.com)" <> wrote in message
news:...
>
> 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


You cannot use @attr, @attr is reserved for instance variables. Also, it's
a bad idea to use instance variables (@tmparray, @tmphash) as temporary
variables.

> Where the method name is the @attr value.


It's a one liner:

objects.sort_by {|o| o.send(attr)}

Kind regards

robert

 
Reply With Quote
 
 
 
 
heinous (sent by Nabble.com)
Guest
Posts: n/a
 
      03-16-2006

> You cannot use @attr, @attr is reserved for instance variables. Also, it's
> a bad idea to use instance variables (@tmparray, @tmphash) as temporary
> variables.


Gotcha there, I wasn't planning to, it was more to make the variable more
obvious in the example.

> It's a one liner:
> objects.sort_by {|o| o.send(attr)}


Exactly what I needed, thanks... Now we're just looking at something like:

def sort_obj(objects,attr)
objects.sort_by {|o| o.send(attr)}
end
--
View this message in context: http://www.nabble.com/Evaluating-the....html#a3442515
Sent from the ruby-talk forum at Nabble.com.



 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      03-17-2006

"heinous (sent by Nabble.com)" <> wrote in message
news:...
>
>> You cannot use @attr, @attr is reserved for instance variables. Also,
>> it's
>> a bad idea to use instance variables (@tmparray, @tmphash) as temporary
>> variables.

>
> Gotcha there, I wasn't planning to, it was more to make the variable more
> obvious in the example.


It's generally preferred to use pieces of code that are syntactically
correct and do something. That makes everyone's lives easier.

>> It's a one liner:
>> objects.sort_by {|o| o.send(attr)}

>
> Exactly what I needed, thanks... Now we're just looking at something
> like:
>
> def sort_obj(objects,attr)
> objects.sort_by {|o| o.send(attr)}
> end


Not really worth a method IMHO bur YMMV.

Kind regards

robert

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
about variable name and method name Ruby Newbee Ruby 1 01-21-2010 08:43 AM
adding a variable name to a hash to name is part of the variable name Bobby Chamness Perl 2 04-22-2007 09:54 PM
Cookbook2 tutorial error: The error occured while evaluating nil.name thundercleesed@gmail.com Ruby 3 02-06-2006 04:43 AM
newbie question on evaluating a string variable John Boik C Programming 2 08-16-2003 07:03 PM
Re: Urgent! how to get object name, method name and attribute name based on the strings? ding feng C++ 2 06-25-2003 01:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57