From: "David A. Black" Date: 2008-04-30T21:59:27+09:00 Subject: Re: How to create "def method(item)= (value)" ? --1926193751-1121908749-1209560367=:9147 Content-Type: MULTIPART/MIXED; BOUNDARY="1926193751-1121908749-1209560367=:9147" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --1926193751-1121908749-1209560367=:9147 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Hi -- On Wed, 30 Apr 2008, Phillip Gawlowski wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > I=C3=B1aki Baz Castillo wrote: > | Hi, is it not possible to create a method to use in this way? > | > | my_object.param("tag") =3D "new_value" > | > | How to define that method? I've tryed something as: > | > | def param(name)=3D (value) > | @params[name] =3D value > | end > | > | but obviously I get error: > | > | SyntaxError: compile error > | syntax error, unexpected '=3D' > | > | Is there some way? Thanks a lot. > | > > Untested, and probably highly inelegant: > > def param key, value > ~ @params["#{key.to_sym}"] =3D value > end > > Should produce: > > | object.params my_key, 'my value' > =3D> @params[:my_key] =3D> 'my value' "#{key.to_sym}" is always going to produce a string, never a symbol. I'm not sure the OP wants to constrain the keys to be symbols anyway. Also, you're calling params; I think you mean param. So: def param(key,value) @params[key] =3D value end object.param(my_key, "my_value") Also untested, due to laziness :-) You could also do: def param[]=3D(key,value) @params[key] =3D value end object.param[key] =3D value Of course, then one might as well do: attr_reader :params on the object, and them: object.params[key] =3D value David --=20 Rails training from David A. Black and Ruby Power and Light: INTRO TO RAILS June 9-12 Berlin ADVANCING WITH RAILS June 16-19 Berlin INTRO TO RAILS June 24-27 London (Skills Matter) See http://www.rubypal.com for details and updates! --1926193751-1121908749-1209560367=:9147-- --1926193751-1121908749-1209560367=:9147--