I'm new to ruby and I'm currently testing the language. So I know I can
write this:
def hello()
yield "hello world"
end
hello { |i| puts i }
This prints "hello world" on the console, cool.
Now I wonder if the following code is valid :
p = Proc.new { yield "hello world" }
p.call { |i| puts i }
when I execute this I get the following error :
LocalJumpError: no block given
from (irb):83
from (irb):85:in `call'
from (irb):85
I belived Proc.call was equivalent to a standard method call, it seems
I'm wrong :). Did I misunderstand something? Can someone tell me where I
made a mistake?
I don't think what you are trying to do is really possible and if it is
there's some odd syntax to make it work. Why are you trying to pass blocks
into procs?
I'm new to ruby and I'm currently testing the language. So I know I can
write this:
def hello()
yield "hello world"
end
hello { |i| puts i }
This prints "hello world" on the console, cool.
Now I wonder if the following code is valid :
p = Proc.new { yield "hello world" }
p.call { |i| puts i }
when I execute this I get the following error :
LocalJumpError: no block given
from (irb):83
from (irb):85:in `call'
from (irb):85
from :0
I belived Proc.call was equivalent to a standard method call, it seems
I'm wrong :). Did I misunderstand something? Can someone tell me where I
made a mistake?
p = Proc.new { yield "hello world" }
p.call { |i| puts i }
Proc is an objectified block that carries the context at the point.
Since no block is given at the point of Proc.new, it raises
LocalJumpError.
I belived Proc.call was equivalent to a standard method call, it seems
I'm wrong :). Did I misunderstand something? Can someone tell me where I
made a mistake?
No, Proc#call is equivalent of yield.
matz.
···
In message "Re: using Proc and yield" on Sat, 16 Dec 2006 00:05:19 +0900, Alber Eric <alberthier@yahoo.fr> writes:
I will just expose what I'm trying to do : I have four differents
methods that are yielding strings. I want to use one or the other
depending on the context but the rest of the process is the same, so I
wrote something like that
p1 = nil
p2 = nil
# First argument handling
case arg1
when 1
p1 = Proc.new { methodI { |s| yield s } }
when 2
p1 = Proc.new { methodII { |s| yield ("foo-" + s + "-bar") } }
end
# Second argument handling
case arg2
when 3
p2 = Proc.new { methodIII { |s| yield s } }
when 4
p1 = Proc.new { methodIV { |s| yield ("oof-" + s + "-rab") } }
end
p1.call { |i|
p2.call { |j|
process(i)
}
}
What I'm looking for is a sort of function pointer.
I will just expose what I'm trying to do : I have four differents
methods that are yielding strings. I want to use one or the other
depending on the context but the rest of the process is the same, so I
wrote something like that
p1 = nil
p2 = nil
# First argument handling
case arg1
when 1
p1 = Proc.new { methodI { |s| yield s } }
when 2
p1 = Proc.new { methodII { |s| yield ("foo-" + s + "-bar") } }
end
# Second argument handling
case arg2
when 3
p2 = Proc.new { methodIII { |s| yield s } }
when 4
p1 = Proc.new { methodIV { |s| yield ("oof-" + s + "-rab") } }
end
p1.call { |i|
p2.call { |j|
process(i)
}
}
What I'm looking for is a sort of function pointer.
I will just expose what I'm trying to do : I have four differents
methods that are yielding strings. I want to use one or the other
depending on the context but the rest of the process is the same, so I
wrote something like that
[...]
What I'm looking for is a sort of function pointer.
I can't really follow what you're doing here, though it does seem a bit
convoluted for something that might be simple. Could you describe what you
want to do in a little more detail?