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