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?
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
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?
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…
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.
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?
=20
I’m trying to define an operator that takes a block, like:
=20
a << {puts “Action!”}
why not:
a << proc { puts “Action!” }
–=20
I’m trying create an HDL(Hardware Description Language) using Ruby (RHDL).
Since most users of HDL’s aren’t
programmers, I’m trying to hide as much Ruby from them as possible (of
course if they really want to learn Ruby that’ll help get more power out
of RHDL, but I don’t want to scare anyone off by saying they need to
learn Ruby first before they can use RHDL). So I don’t want the users
to have to know about procs.
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…
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.
I’m wondering if this is some sort of bug?
Phil
I was just talking to some folks on IRQ and Apparently it’s not a bug,
you’ve got to do:
f.<< {puts “An Action”}
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.
I’m wondering if this is some sort of bug?
I was just talking to some folks on IRQ and Apparently it’s not a bug,
you’ve got to do:
f.<< {puts “An Action”}
The ‘.’ is needed in this case.
Yeah, but in that case you’re just calling << directly as if it was a function,
so why not just MAKE a function:
f.add {puts “an Action”}
Having a method with the same name as an operator << is useful so that ruby
hooks statements like
a << b
and converts the statement to
a.<<(b)
I think having a method called << that can’t actually be used as
a << b
is pretty confusing!
I guess its not considered a bug because a block isn’t an arg, and the ruby syntax
doesn’t allow blocks on binary operators, it appears, I just tried!
[ensemble] ~/p/ruby/vcard $ irb
irb(main):001:0> class T; def <<(arg); yield “hi #{arg}”; end; end
nil
irb(main):002:0> t = T.new
#<T:0x170a1c>
irb(main):005:0> t << 5 { |y| p y }
SyntaxError: compile error
(irb):5: parse error
t << 5 { |y| p y }
^
from (irb):5