From: "David A. Black" Date: 2008-05-08T23:18:01+09:00 Subject: Re: Why can't I redefine "<<" method to allow two parameters? --1926193751-144822624-1210256276=:24898 Content-Type: MULTIPART/MIXED; BOUNDARY="1926193751-144822624-1210256276=:24898" 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-144822624-1210256276=:24898 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Hi -- On Thu, 8 May 2008, I=C3=B1aki Baz Castillo wrote: > 2008/5/8, Jens Wille : >> Jason Roelofs [2008-05-08 14:50]: >> >>> On Thu, May 8, 2008 at 8:42 AM, I=C3=B1aki Baz Castillo = wrote: >> the point is that you can't call an operator with multiple >> arguments. maybe something like this will suit you (note that you >> don't need to alias if you inherit from Array): >> >> class MyArray < Array >> def <<(*a) >> a =3D [*(a=3D*a)] # a.to_a.flatten_once ;-) >> raise ArgumentError, "wrong number of arguments (#{a.size} for >> 2)" if a.size !=3D 2 >> >> super a.join(': ') >> >> end >> end >> >> my_array =3D MyArray.new >> >> my_array << ['Header1', 'Value1'] # =3D> ["Header1: Value1"] >> my_array.<<('Header2', 'Value2') # =3D> ["Header1: Value1", >> "Header2: Value2"] > > > Thanks to all. Finally I'll just create a "add" method that will call > internally to << method using appropiate syntax: > self.<< (k, v) I would just use Array#push: >> [1,2,3].push(4,5) =3D> [1, 2, 3, 4, 5] 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-144822624-1210256276=:24898-- --1926193751-144822624-1210256276=:24898--