From: Robert Klemme Date: 2008-05-08T22:24:22+09:00 Subject: Re: Why can't I redefine "<<" method to allow two parameters? 2008/5/8 Iņaki Baz Castillo : > 2008/5/8, Iņaki Baz Castillo : >> 2008/5/8, Iņaki Baz Castillo : >> >> > 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 .<< > => ["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 -- use.inject do |as, often| as.you_can - without end