ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> ruby-talk
ruby-talk
Re: Why can't I redefine "<<" method to allow two parameters?
by Jason Roelofs other posts by this author
May 8 2008 5:50AM messages near this date
Re: Why can't I redefine "<<" method to allow two parameters? | Re: Why can't I redefine "<<" method to allow two parameters?
On Thu, May 8, 2008 at 8:42 AM, Iñaki Baz Castillo <ibc@[...].net>  wrote:
>  Hi, very exrtange:
> 
> 
>  class MyArray < Array
>   alias original_add <<
>   def <<(n,k)
>     original_add "#{n}: #{k}"
>   end
>  end
> 
>  my_array = MyArray.new
>  => []
> 
>  my_array << ("Header", "Value")
> 
>  SyntaxError: compile error
>  (irb):25: syntax error, unexpected ',', expecting ')'
>  my_array << ("Header", "Value")
>                          ^
> 
> 
>  Any reason for this? It seems that << is a littled "hardcoded", isn't?
> 
>  --
>  Iñaki Baz Castillo
>  <ibc@[...].net>
> 

The << operator is a special case handled by the parser, but you can
bypass that handling by calling the method directly:

my_array.<<("Header", "Value")

which will of course look a ton better by just using a custom method:

my_array.add_stuff "Header", "Value"

Jason R.
Thread:
ibc
Rick DeNatale
ibc
ibc
Robert Klemme
Jason Roelofs
Jens Wille
ibc
David A. Black
Rick DeNatale
Robert Klemme
Jimmy Kofler
David A. Black
Phillip Gawlowski

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved