From: Rick DeNatale Date: 2008-05-09T01:07:11+09:00 Subject: Re: Why can't I redefine "<<" method to allow two parameters? On Thu, May 8, 2008 at 10:18 AM, David A. Black wrote: > Hi -- > On Thu, 8 May 2008, Iņaki 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ņaki 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 = [*(a=*a)] # a.to_a.flatten_once ;-) > > > raise ArgumentError, "wrong number of arguments (#{a.size} for > > > 2)" if a.size != 2 > > > > > > super a.join(': ') > > > > > > end > > > end > > > > > > my_array = MyArray.new > > > > > > my_array << ['Header1', 'Value1'] # => ["Header1: Value1"] > > > my_array.<<('Header2', 'Value2') # => ["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) > > > > > > => [1, 2, 3, 4, 5] We seem to have lost track of what the OP was trying to do, class MyArray < Array alias original_add << def <<(n,k) original_add "#{n}: #{k}" end end He wants [1,2,3].whatever_the_heck_he_decides_to_call_it(4,5) #=> [1,2,3, "4:5"] -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/