From: ptkwt@...1.aracnet.com (Phil Tomson) Date: 2003-03-15T15:44:07+09:00 Subject: Re: Overloaded operator interprets block as hash (bug?) In article <3E729E3F.7020007@day-reynolds.com>, Lennon Day-Reynolds wrote: >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 True, but... 1) I'm sort of creating a domain specific language from Ruby (an HDL) and I'm trying to hide that sort of thing from the user. 2) I'm wondering if this is some sort of bug? Phil > >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 >> >> >> > >