[#1611] set_trace_func/Array#fetch error — "Nathaniel Talbott" <nathaniel@...>

I've reduced the error I reported in ruby-talk:84013 to the following code:

17 messages 2003/10/11

Re: Yielding to a block from a proc?

From: george.marrows@...
Date: 2003-10-08 12:49:07 UTC
List: ruby-core #1591
> > Is this right? Is this pathological? Is it a bug? Is there 
> a reason that 
> > this
> > isn't possible, other than the fact that it's pathological 
> to want to do so?
> 
> I don't think it's pathological to want to do this:
> 
>    class << self
>      instance_eval {
>        define_method(:foo) { |a, &b| b[a] }
>      }
>    end
> 
>    #The above should have the same effect as:
>    #def self.foo(a, &b); b[a]; end
> 
>    p(foo(3) {|x| x+1})

I agree that the above example is something you might well want to do, but,
putting aside syntax issues, the problem is that procs already have blocks
to yield to - they capture the block (if any) that was in scope when they
were created:

def test
	proc { yield 123 }   # <-- captures block passed to test
end

p = test { |a| puts "Got #{a}" }

p.call()

Though this is fairly weird (does anyone use it?), it's entirely in keeping
with the fact that blocks have access to the blocks that were passed into
their scope:

def test2(a)
	a.each { |x|
		yield x    # <-- accesses block passed to test2
	}
end

test2([1,2]) { |y| puts y }

-- George

In This Thread

Prev Next