Dynamic method name...was RE: hash question

First – thanks Guy, that solved that problem and allowed me to find and
move on to the next.

Let’s say I have a reference to an object called “contact” and it has an
attribute “phone”. The atrribute phone can be legally set like this:

contact.phone = “703-277-7272”

But, let’s say I have a variable, called meth. And meth == “phone”. I want
to use my variable meth like this:

contact.meth = “703-277-7272” # this should set phone

I tried using @meth, but I get a parse error. How would I go about doing
this?

-Dwayne

···

-----Original Message-----
From: ts [mailto:decoux@moulon.inra.fr]
Sent: Sunday, February 02, 2003 6:04 AM
To: ruby-talk ML
Cc: ruby-talk@ruby-lang.org
Subject: Re: hash question…

“D” == Dwayne Smurdon @ DNA Media Pro
smurdon@dnamediapro.com writes:

map.keys.each |a| { person.a = rs.Fields(map[a]).Value }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This is interpreted as an Hash, you must write

map.keys.each {|a| person.a = rs.Fields(map[a]).Value }
^^^^

now it’s interpreted as a block

D:/Apps/Ruby/projects/OutlookContact.rb:29: odd number
list for Hash

This error message is for this

pigeon% ruby -e ‘hash = { 1 }’
-e:1: odd number list for Hash
pigeon%

pigeon% ruby -e ‘hash = { 1, 2 }’
pigeon%

pigeon% ruby -e ‘hash = { 1, 2, 3 }’
-e:1: odd number list for Hash
pigeon%

Guy Decoux

contact.phone = "703-277-7272"

When you write this, you call the method #phone=

This is *like* if you write contact.phone=("703-277-7272")

But, let's say I have a variable, called meth. And meth == "phone". I want
to use my variable meth like this:

Use #send

   contact.send("#{meth}=", "703-277-7272")

Guy Decoux

And if you do this a lot, you can get struct-like behaviour using

def =(meth, args)
send(“#{meth}=”, args)
end

and call it with contact[‘phone’] = “wha-te-ver”

martin

···

ts decoux@moulon.inra.fr wrote:

contact.phone = “703-277-7272”

Use #send

contact.send(“#{meth}=”, “703-277-7272”)