# Using Proc and yield

**URL:** <https://www.ruby-forum.com/t/using-proc-and-yield/79480>\
**Category:** Ruby\
**Created:** [December 15, 2006, 4:05pm UTC](https://www.ruby-forum.com/t/using-proc-and-yield/79480 "2006-12-15T16:05:17Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![alberthier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a9adbd/32.png) [@alberthier](https://www.ruby-forum.com/u/alberthier)\
**Post date:** [December 15, 2006, 4:05pm UTC](https://www.ruby-forum.com/t/using-proc-and-yield/79480/1 "2006-12-15T16:05:17Z")

</div>

hi !

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?

Thank you

---

<div class="post-metadata">

**Author:** ![alberthier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a9adbd/32.png) [@alberthier](https://www.ruby-forum.com/u/alberthier)\
**Post date:** [December 15, 2006, 4:19pm UTC](https://www.ruby-forum.com/t/using-proc-and-yield/79480/2 "2006-12-15T16:19:06Z")

</div>

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?

Jason

---

<div class="post-metadata">

**Author:** ![alberthier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a9adbd/32.png) [@alberthier](https://www.ruby-forum.com/u/alberthier)\
**Post date:** [December 15, 2006, 4:21pm UTC](https://www.ruby-forum.com/t/using-proc-and-yield/79480/3 "2006-12-15T16:21:07Z")

</div>

Hi,

In message “Re: using Proc and yield”  
on Sat, 16 Dec 2006 00:05:19 +0900, Alber E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
writes:

|Now I wonder if the following code is valid :  
|  
|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.

```

---

<div class="post-metadata">

**Author:** ![alberthier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a9adbd/32.png) [@alberthier](https://www.ruby-forum.com/u/alberthier)\
**Post date:** [December 15, 2006, 5:00pm UTC](https://www.ruby-forum.com/t/using-proc-and-yield/79480/4 "2006-12-15T17:00:13Z")

</div>

OK, thank you for your quick answer

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.

Thanks

Eric

---

<div class="post-metadata">

**Author:** ![alberthier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a9adbd/32.png) [@alberthier](https://www.ruby-forum.com/u/alberthier)\
**Post date:** [December 15, 2006, 5:23pm UTC](https://www.ruby-forum.com/t/using-proc-and-yield/79480/5 "2006-12-15T17:23:29Z")

</div>

At the end, read process(i, j) instead of process(i)

---

<div class="post-metadata">

**Author:** ![alberthier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a9adbd/32.png) [@alberthier](https://www.ruby-forum.com/u/alberthier)\
**Post date:** [December 15, 2006, 5:37pm UTC](https://www.ruby-forum.com/t/using-proc-and-yield/79480/6 "2006-12-15T17:37:26Z")

</div>

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?

Jason

---

<div class="post-metadata">

**Author:** ![alberthier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a9adbd/32.png) [@alberthier](https://www.ruby-forum.com/u/alberthier)\
**Post date:** [December 15, 2006, 6:11pm UTC](https://www.ruby-forum.com/t/using-proc-and-yield/79480/7 "2006-12-15T18:11:32Z")

</div>

On 12/15/06, Alber E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> # First argument handling
> 
> ```
> p2 = Proc.new { methodIII { |s| yield s } }
> 
> ```
> 
> What I’m looking for is a sort of function pointer.

def p1( arg )  
yield arg  
end

def p2( arg )  
yield arg  
end

# First argument handling

case arg1  
when 1: nil # p1 already defined correctly  
when 2  
eval %(  
def p1( arg )  
yield( “foo-” + arg + “-bar” )  
end  
)  
end

# Second argument handling

case arg2  
when 3: nil # p2 already defined correctly  
when 4  
eval %(  
def p1( arg )  
yield (“oof-” + arg + “-rab”)  
end  
)  
end

p1( arg1 ) do |i|  
p2( arg2 ) do |j|  
process(i,j)  
end  
end

No need for function pointers. Just define (or redefine) the methods  
as you need them.

Blessings,  
TwP

---

<div class="post-metadata">

**Author:** ![alberthier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a9adbd/32.png) [@alberthier](https://www.ruby-forum.com/u/alberthier)\
**Post date:** [December 16, 2006, 4:31am UTC](https://www.ruby-forum.com/t/using-proc-and-yield/79480/8 "2006-12-16T04:31:10Z")

</div>

On 12/16/06, Alber E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> OK, thank you for your quick answer
> 
> 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.

Hi Eric,

Blocks can’t take blocks in 1.8, so I think you’ll need to resort  
to methods if you do it this way. Example:

def methodI\_foo\_bar  
methodI{|s| yield “foo-#{s}-bar”}  
end  
def methodIII\_oof\_rab  
methodIII{|s| yield “oof-#{s}-rab”}  
end

# First argument handling

case arg1  
when 1  
p1 = method(:methodI)  
when 2  
p1 = method(:methodI\_foo\_bar)  
end

# Second argument handling

case arg2  
when 3  
p2 = method(:methodIII)  
when 4  
p2 = method(:methodIII\_oof\_rab)  
end

p1.call { |i|  
p2.call { |j|  
process(i, j)  
}  
}

Or, perhaps more idiomatically:

def methodI\_foo\_bar  
methodI{|s| yield “foo-#{s}-bar”}  
end  
def methodIII\_oof\_rab  
methodIII{|s| yield “oof-#{s}-rab”}  
end

# First argument handling

case arg1  
when 1  
m1 = :methodI  
when 2  
m1 = :methodI\_foo\_bar  
end

# Second argument handling

case arg2  
when 3  
m2 = :methodIII  
when 4  
m2 = :methodIII\_oof\_rab  
end

send(m1) { |i|  
send(m2) { |j|  
process(i, j)  
}  
}

Does that help?
