From: Lennon Day-Reynolds Date: 2003-03-15T12:29:50+09:00 Subject: Re: Overloaded operator interprets block as hash Every variation I've tried of passing a block as a parameter to an operator fails with a syntax error, so at least on 1.6.8/Win32, there doesn't appear to be any syntactic support for what you're trying to do. Of course, you could just have the operator accept a Proc instance, rather than a bare block, as a parameter, at the cost of five extra characters for each call. Lennon Phil Tomson wrote: >I'm trying to define an operator that takes a block, like: > >a << {puts "Action!"} > >but the part in '{}' gets interpreted as a hash. Here's the sample code: >class Foo > def initialize(&proc) > @closure = proc > end > > def assign(newVal = Proc.new) > @closure = newVal > end > > def <<(newVal = Proc.new) > @closure = newVal > end > def callit > @closure.call > end >end > >f= Foo.new { puts "Action!" } >f.callit #=> "Action!" >f.assign { puts "New Action!" } >f.callit #=> "New Action!" >f<< { puts "Latest Action!" } > >SyntaxError: compile error >(irb):26: parse error >f << { puts "Latest Action!" } > ^ > from (irb):26 > >.... it seems as though the block being passed to '<<' is getting >interpreted as a hash while it isn't for the 'assign' method. Is there >anyway around this? > > >Phil > > >