Robert Klemme
5/8/2008 1:24:00 PM
2008/5/8 I=F1aki Baz Castillo <ibc@aliax.net>:
> 2008/5/8, I=F1aki Baz Castillo <ibc@aliax.net>:
>> 2008/5/8, I=F1aki Baz Castillo <ibc@aliax.net>:
>>
>> > Any reason for this? It seems that << is a littled "hardcoded", isn't=
?
>>
>>
>> Sorry, it was a failure of mine. It works ok.
>
>
> Nooooooooo, it doesn't work OK at all!!!
>
> It just works if I call to the redefined << method with DOT and
> arguments between ( ):
>
> my_array.<<("hola","pepe") # Note the DOT in .<<
> =3D> ["hola: pepe"]
>
> my_array<<("hola","pepe") # Without DOT
> SyntaxError: compile error
> (irb):11: syntax error, unexpected ',', expecting ')'
> my_array<<("hola","pepe")
> ^
> from (irb):11
> from :0
>
> Why??
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:
15:23:08 $ ruby -ce '1 << 2'
Syntax OK
15:23:11 $ ruby -ce '1 << 2 3'
-e:1: syntax error, unexpected tINTEGER, expecting $end
15:23:14 $ ruby -ce '1 << 2,3'
-e:1: syntax error, unexpected ',', expecting $end
1 << 2,3
^
15:23:16 $
HTH
Kind regards
robert
--=20
use.inject do |as, often| as.you_can - without end