# How to check for block in an evaled method?

**URL:** <https://www.ruby-forum.com/t/how-to-check-for-block-in-an-evaled-method/113060>\
**Category:** Ruby\
**Created:** [August 27, 2007, 4:16am UTC](https://www.ruby-forum.com/t/how-to-check-for-block-in-an-evaled-method/113060 "2007-08-27T04:16:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nasir\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/838e76/32.png) [@Nasir\_K](https://www.ruby-forum.com/u/Nasir_K)\
**Post date:** [August 27, 2007, 4:16am UTC](https://www.ruby-forum.com/t/how-to-check-for-block-in-an-evaled-method/113060/1 "2007-08-27T04:16:28Z")

</div>

Consider the snippet -

class B  
def set  
B.class\_eval do  
define\_method(:a) do |arg|  
if block\_given?  
yield  
else  
“hello”  
end  
end  
end  
end  
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@:45(irb)  
irb(main):060:0\> y.a  
(irb):45: warning: multiple values for a block parameter (0 for 1)  
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)  
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@:45(irb)  
irb(main):063:0\> y.a  
(irb):45: warning: multiple values for a block parameter (0 for 1)  
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:** ![Nasir\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/838e76/32.png) [@Nasir\_K](https://www.ruby-forum.com/u/Nasir_K)\
**Post date:** [August 27, 2007, 5:30am UTC](https://www.ruby-forum.com/t/how-to-check-for-block-in-an-evaled-method/113060/2 "2007-08-27T05:30:46Z")

</div>

On 8/26/07, Nasir K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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  
}

Alternative route is to use string eval.

---

<div class="post-metadata">

**Author:** ![Nasir\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/838e76/32.png) [@Nasir\_K](https://www.ruby-forum.com/u/Nasir_K)\
**Post date:** [August 27, 2007, 12:43pm UTC](https://www.ruby-forum.com/t/how-to-check-for-block-in-an-evaled-method/113060/3 "2007-08-27T12:43:50Z")

</div>

Hi –

On Mon, 27 Aug 2007, Logan C. wrote:

> On 8/26/07, Nasir K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > 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:

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

c = C.new  
c.foo(3) { puts “hi” }

which gives me:

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

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

David

---

<div class="post-metadata">

**Author:** ![Nasir\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/838e76/32.png) [@Nasir\_K](https://www.ruby-forum.com/u/Nasir_K)\
**Post date:** [August 28, 2007, 1:42am UTC](https://www.ruby-forum.com/t/how-to-check-for-block-in-an-evaled-method/113060/4 "2007-08-28T01:42:43Z")

</div>

On 8/27/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Stage 1) Upgrade to ruby 1.9 or later. 2) define\_method(:foo) { |arg, &block|  
> > end  
> > 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…?
