From: "David A. Black" Date: 2008-04-30T22:52:13+09:00 Subject: Re: How to create "def method(item)= (value)" ? --1926193751-937493353-1209563533=:10337 Content-Type: MULTIPART/MIXED; BOUNDARY="1926193751-937493353-1209563533=:10337" 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-937493353-1209563533=:10337 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: > | 2008/4/30, Phillip Gawlowski : > |> | You could also do: > |> | > |> | def param[]=3D(key,value) > |> | @params[key] =3D value > |> | end > |> | > |> | object.param[key] =3D value > |> > |> Oh, I'm gonna steal that idiom if you don't mind. ;) > | > | I'm sorry, but as I've replied in this thread that method naming > | (using []) is invalid in Ruby :( > > Sort of. As a singleton method, you get the syntax error. But not if you > define [] as a method: > > irb(main):001:0> def []=3D(key,value) > irb(main):002:1> end > =3D> nil It's not the singletonness that stops it; you can do this: def object.[]=3D(k,v) # etc. What made my example wrong was that I had concocted an illegal method name. 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-937493353-1209563533=:10337-- --1926193751-937493353-1209563533=:10337--