The parser sees the << OPERATOR as taking a left and right argument,
and turns that into a message send to the left argument. If you want
to use additional arguments you need to be a bit more explicit and use
a little less syntactic sugar:
Any reason for this? It seems that << is a littled "hardcoded", isn't?
--
Iñaki Baz Castillo
<ibc@aliax.net>
The << operator is a special case handled by the parser, but you can
bypass that handling by calling the method directly:
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
As others have pointed out it is not an issue of method #<< but of the
syntax. In other words, the syntax allows for << just one argument to
the left and one to the right (similar to #+ and all other _binary_
operators). While you can define the method #<< to accept any number
of arguments the parser simply won't call it with more than one
argument because that is a syntax error:
Thanks to all. Finally I'll just create a "add" method that will call
internally to << method using appropiate syntax:
self.<< (k, v)
Thanks.
···
2008/5/8, Jens Wille <jens.wille@uni-koeln.de>:
Jason Roelofs [2008-05-08 14:50]:
> On Thu, May 8, 2008 at 8:42 AM, Iñaki Baz Castillo <ibc@aliax.net> 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
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
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]
David
···
On Thu, 8 May 2008, Iñaki Baz Castillo wrote:
2008/5/8, Jens Wille <jens.wille@uni-koeln.de>:
On Thu, May 8, 2008 at 8:42 AM, Iñaki Baz Castillo <ibc@aliax.net> wrote:
--
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!
Why can't I redefine "<<" method to allow two parameters?
Posted by Iñaki Baz Castillo (Guest) on 08.05.2008 14:42
Hi, very exrtange:
...
my_array << ("Header", "Value")
On Thu, May 8, 2008 at 10:18 AM, David A. Black <dblack@rubypal.com> wrote:
Hi --
On Thu, 8 May 2008, Iñaki Baz Castillo wrote:
> 2008/5/8, Jens Wille <jens.wille@uni-koeln.de>:
>
> > Jason Roelofs [2008-05-08 14:50]:
> >
> >
> > > On Thu, May 8, 2008 at 8:42 AM, Iñaki Baz Castillo <ibc@aliax.net> > 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)
>
Why can't I redefine "<<" method to allow two parameters?
Posted by Iñaki Baz Castillo (Guest) on 08.05.2008 14:42
Hi, very exrtange:
...
my_array << ("Header", "Value")
This seems to work as well:
ruby -e 'p [1,2,3] << 4 << 5'
#=> [1, 2, 3, 4, 5]
What do you guys have against #push?
David
--
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!
~ - You know you've been hacking too long when...
...you almost get hit by a bus that pulled away from the stop without
looking and you say: PANIC: bus error