Overloaded operator interprets block as hash

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

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

why not:

a << proc { puts “Action!” }

···

Phil Tomson (ptkwt@shell1.aracnet.com) wrote:

I’m trying to define an operator that takes a block, like:

a << {puts “Action!”}


Eric Hodel - drbrain@segment7.net - http://segment7.net
All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

what about:

class Foo
def initialize(p)
self <<(p)
end

def <<(p)
@closure = p
end

def callit(*args)
@closure.call(*args)
end
end

f = Foo.new proc { puts “bar” }
f.callit

f << proc { puts “hello” }
f.callit

robert

“Phil Tomson” ptkwt@shell1.aracnet.com schrieb im Newsbeitrag
news:b4tt9l01nee@enews1.newsguy.com

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

In article 3E729E3F.7020007@day-reynolds.com,

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

···

Lennon Day-Reynolds lennon@day-reynolds.com wrote:

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

In article 20030316001530.GG67470@segment7.net,

–vK3aNhhOnFYnG5nq
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

=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.

Phil

···

Eric Hodel drbrain@segment7.net wrote:

Phil Tomson (ptkwt@shell1.aracnet.com) wrote:

In article b4ughr02k6f@enews4.newsguy.com,

···

Phil Tomson ptkwt@shell1.aracnet.com wrote:

In article 3E729E3F.7020007@day-reynolds.com,
Lennon Day-Reynolds lennon@day-reynolds.com 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

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.

Phil

Quoteing ptkwt@shell1.aracnet.com, on Sat, Mar 15, 2003 at 05:44:40PM +0900:

In article b4ughr02k6f@enews4.newsguy.com,

In article 3E729E3F.7020007@day-reynolds.com,
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?

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! :slight_smile:

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

Cheers,
Sam

···

Phil Tomson ptkwt@shell1.aracnet.com wrote:

Lennon Day-Reynolds lennon@day-reynolds.com wrote: