[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Chris G (sent by Nabble.com)

3/16/2006 5:23:00 PM


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-string-in-a-variable-to-use-as-a-method-name-t1292849.htm...
Sent from the ruby-talk forum at Nabble.com.



3 Answers

Robert Klemme

3/16/2006 5:38:00 PM

0


"heinous (sent by Nabble.com)" <lists@nabble.com> wrote in message
news:3440567.post@talk.nabble.com...
>
> 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

Chris G (sent by Nabble.com)

3/16/2006 7:05:00 PM

0


> 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-string-in-a-variable-to-use-as-a-method-name-t1292849.htm...
Sent from the ruby-talk forum at Nabble.com.



Robert Klemme

3/17/2006 11:04:00 AM

0


"heinous (sent by Nabble.com)" <lists@nabble.com> wrote in message
news:3442515.post@talk.nabble.com...
>
>> 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