# How to check for block in an evaled method?

**URL:** <https://rubytalk.org/t/how-to-check-for-block-in-an-evaled-method/40206>\
**Category:** ruby-talk\
**Created:** [27 August 2007 02:15 UTC](https://rubytalk.org/t/how-to-check-for-block-in-an-evaled-method/40206 "2007-08-27T02:15:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nasir\_Khan](https://avatars.discourse-cdn.com/v4/letter/n/5daacb/32.png) [@Nasir\_Khan](https://rubytalk.org/u/Nasir_Khan)\
**Post date:** [27 August 2007 02:15 UTC](https://rubytalk.org/t/how-to-check-for-block-in-an-evaled-method/40206/1 "2007-08-27T02:15:01Z")

</div>

Consider the snippet -

class B  
&nbsp;&nbsp;&nbsp;def set  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;B.class\_eval do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method(:a) do |arg|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if block\_given?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"hello"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

The intention is that block\_given? would tell me if the new method a() is  
invoked with a block or not.  
Now I run this

irb(main):057:0\* y = B.new  
=\> #\<B:0x2caf2b8\>

irb(main):059:0\> y.set  
=\> #\<Proc:0x02cb6784@(irb):45\>  
irb(main):060:0\> y.a  
(irb):45: warning: multiple values for a block parameter (0 for 1)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):60  
=\> "hello"

Now if I use with a block

irb(main):061:0\> y.a {"bye"}  
(irb):45: warning: multiple values for a block parameter (0 for 1)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):61  
=\> "hello"

Oops doesnt work !

Actually the block is being evaled when the method is being created -

irb(main):062:0\> y.set {"bye"}  
=\> #\<Proc:0x02cb6784@(irb):45\>  
irb(main):063:0\> y.a  
(irb):45: warning: multiple values for a block parameter (0 for 1)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):63  
=\> "bye"

I get that. But the question is, is there a way to check for a block and  
then yield in a defined method, as was my naive intention?

Thanks  
Nasir

---

<div class="post-metadata">

**Author:** ![Logan\_Capaldo](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@Logan\_Capaldo](https://rubytalk.org/u/Logan_Capaldo)\
**Post date:** [27 August 2007 03:30 UTC](https://rubytalk.org/t/how-to-check-for-block-in-an-evaled-method/40206/2 "2007-08-27T03:30:22Z")

</div>

> I get that. But the question is, is there a way to check for a block and  
> then yield in a defined method, as was my naive intention?

Stage 1) Upgrade to ruby 1.9 or later. 2) define\_method(:foo) { |arg, &block|  
&nbsp;&nbsp;if block then block.call else puts arg  
}

Alternative route is to use string eval.

> **···**
>
> On 8/26/07, Nasir Khan \<rubylearner@gmail.com\> wrote:
> 
> > Thanks  
> > Nasir

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [27 August 2007 10:43 UTC](https://rubytalk.org/t/how-to-check-for-block-in-an-evaled-method/40206/3 "2007-08-27T10:43:18Z")

</div>

Hi --

> > I get that. But the question is, is there a way to check for a block and  
> > then yield in a defined method, as was my naive intention?
> 
> Stage 1) Upgrade to ruby 1.9 or later. 2) define\_method(:foo) { |arg, &block|  
> if block then block.call else puts arg  
> }

I'm not sure. Here's my little test:

&nbsp;&nbsp;&nbsp;class C  
&nbsp;&nbsp;&nbsp;define\_method(:foo) { |arg, &block|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if block then block.call else puts arg end  
&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;c = C.new  
&nbsp;&nbsp;&nbsp;c.foo(3) { puts "hi" }

which gives me:

&nbsp;&nbsp;&nbsp;$ /usr/local/lib/ruby-svn/bin/ruby -v block9.rb  
&nbsp;&nbsp;&nbsp;ruby 1.9.0 (2007-08-25 patchlevel 0) [i686-darwin8.10.1]  
&nbsp;&nbsp;&nbsp;3

This is on 1/2-cup coffee so I may well be not seeing something I'm  
doing wrong.

David

> **···**
>
> On Mon, 27 Aug 2007, Logan Capaldo wrote:
> 
> > On 8/26/07, Nasir Khan \<rubylearner@gmail.com\> wrote:
> 
> --  
> \* Books:  
> &nbsp;&nbsp;&nbsp;RAILS ROUTING (new! [http://www.awprofessional.com/title/0321509242\](http://www.awprofessional.com/title/0321509242%5C))  
> &nbsp;&nbsp;&nbsp;RUBY FOR RAILS ([http://www.manning.com/black\](http://www.manning.com/black%5C))  
> \* Ruby/Rails training  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& consulting: Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![Logan\_Capaldo](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@Logan\_Capaldo](https://rubytalk.org/u/Logan_Capaldo)\
**Post date:** [27 August 2007 23:41 UTC](https://rubytalk.org/t/how-to-check-for-block-in-an-evaled-method/40206/4 "2007-08-27T23:41:33Z")

</div>

> Hi --
> 
> \>  
> \>\>  
> \>\> I get that. But the question is, is there a way to check for a block and  
> \>\> then yield in a defined method, as was my naive intention?  
> \>\>  
> \> Stage 1) Upgrade to ruby 1.9 or later. 2) define\_method(:foo) { |arg, &block|  
> \> if block then block.call else puts arg  
> \> }
> 
> I'm not sure. Here's my little test:
> 
> &nbsp;&nbsp;&nbsp;class C  
> &nbsp;&nbsp;&nbsp;define\_method(:foo) { |arg, &block|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if block then block.call else puts arg end  
> &nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;c = C.new  
> &nbsp;&nbsp;&nbsp;c.foo(3) { puts "hi" }
> 
> which gives me:
> 
> &nbsp;&nbsp;&nbsp;$ /usr/local/lib/ruby-svn/bin/ruby -v block9.rb  
> &nbsp;&nbsp;&nbsp;ruby 1.9.0 (2007-08-25 patchlevel 0) [i686-darwin8.10.1]  
> &nbsp;&nbsp;&nbsp;3
> 
> This is on 1/2-cup coffee so I may well be not seeing something I'm  
> doing wrong.

Well alrighty then... I don't know what thats about and I haven't  
installed 1.9ish in a while. I'm surprised that doesn't work but is  
valid syntax though. I wonder what is actually happening? Assigning  
the (non-existant, and I think impossible) block passed to class C...?

> **···**
>
> On 8/27/07, dblack@wobblini.net \<dblack@wobblini.net\> wrote:
> 
> > On Mon, 27 Aug 2007, Logan Capaldo wrote:  
> > \> On 8/26/07, Nasir Khan \<rubylearner@gmail.com\> wrote:
> > 
> > David
> > 
> > --  
> > \* Books:  
> > &nbsp;&nbsp;&nbsp;RAILS ROUTING (new! [http://www.awprofessional.com/title/0321509242\](http://www.awprofessional.com/title/0321509242%5C))  
> > &nbsp;&nbsp;&nbsp;RUBY FOR RAILS ([http://www.manning.com/black\](http://www.manning.com/black%5C))  
> > \* Ruby/Rails training  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& consulting: Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))
