Forum: Ruby Evaluating the string in a variable to use as a method name

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
heinous (sent by Nabble.com) (Guest)
on 2006-03-16 19:25
(Received via mailing list)
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?(removed_email_address@domain.invalid_s)
        @tmphash[removed_email_address@domain.invalid_s] = Array.new
      end
      @tmphash[removed_email_address@domain.invalid_s].push(object)
    end
  end

Where the method name is the @attr value.
Robert K. (Guest)
on 2006-03-16 19:40
(Received via mailing list)
"heinous (sent by Nabble.com)" <removed_email_address@domain.invalid> wrote in 
message
news:removed_email_address@domain.invalid...
>    @tmphash = Hash.new
>    for object in @objects
>      if ! @tmphash.has_key?(removed_email_address@domain.invalid_s)
>        @tmphash[removed_email_address@domain.invalid_s] = Array.new
>      end
>      @tmphash[removed_email_address@domain.invalid_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
heinous (sent by Nabble.com) (Guest)
on 2006-03-16 21:06
(Received via mailing list)
> 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
Robert K. (Guest)
on 2006-03-17 13:09
(Received via mailing list)
"heinous (sent by Nabble.com)" <removed_email_address@domain.invalid> wrote in 
message
news:removed_email_address@domain.invalid...
>
>> 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
heinous (sent by Nabble.com) (Guest)
on 2006-03-17 16:56
(Received via mailing list)
> It's generally preferred to use pieces of code that are syntactically
> correct and do something.  That makes everyone's lives easier. :-)

Thanks, I will do so in the future.  Sorry about confusion that I may
have
caused.

>>  def sort_obj(objects,attr)
>>    objects.sort_by {|o| o.send(attr)}
>>  end

> Not really worth a method IMHO bur YMMV.

Yeah, suspect you're right there too, but it helps code readibility for
me
as I'm still learning.

Anyway, thanks again for your help, it works like a charm.
This topic is locked and can not be replied to.