# Eval confusion

**URL:** <https://www.ruby-forum.com/t/eval-confusion/70932>\
**Category:** Ruby\
**Created:** [October 4, 2006, 1:01am UTC](https://www.ruby-forum.com/t/eval-confusion/70932 "2006-10-04T01:01:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paolo\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b9e5f3/32.png) [@Paolo\_N](https://www.ruby-forum.com/u/Paolo_N)\
**Post date:** [October 4, 2006, 1:01am UTC](https://www.ruby-forum.com/t/eval-confusion/70932/1 "2006-10-04T01:01:01Z")

</div>

Hi there

I need some help understating this behaviour.  
I’ve read the docs of define\_method but I’m still missing how it exactly  
works.  
Here’s some code I’ve written trying to use to understand

> cat script.rb

#!/bin/ruby  
class DefMethod  
M\_COLLECTION = (‘a’…‘c’)  
M\_COLLECTION.each do |alpha|  
define\_method(alpha) {  
block\_given? ? puts(yield(alpha)) : puts(alpha)  
}  
end  
end

class EvalMethod  
DefMethod::M\_COLLECTION.each do |alpha|  
class\_eval \<\<-ENDEVAL  
def #{alpha}  
block\_given? ? puts(yield(“#{alpha}”)) : puts(“#{alpha}”)  
end  
ENDEVAL  
end  
end

defobj = DefMethod.new  
evalobj = EvalMethod.new  
print “====no block====\n”  
defobj.a  
evalobj.a  
print “====with block====\n”  
defobj.a {|x| x + ’ \<\< block given!‘}  
evalobj.a {|x| x + ’ \<\< block given!’}

Basicly I’m defining 2 class with 3 method (a,b,c) each in the first  
class I use define\_method and in the second class\_eval.  
Now, the output of this script is

> ruby script.rb  
> ====no block====  
> a  
> a  
> ====with block====  
> a  
> a \<\< block given!

So the ‘a’ method is sensible to the block only in the version defined  
with class\_eval.  
After reading the usage of define\_method here

> **[Ruby instance\_exec aka instance\_eval with parameters](http://blog.jayfields.com/2006/09/ruby-instanceexec-aka-instanceeval.html)**
>
> Updated: Changed the instance\_exec implementation to a better version, also written by Mauricio. See comments for more info. Recently we w...

I can understand that my first class is basicly wrong but I would  
still understand what happens behind the scenes, and how I can manage  
to ‘see’ the block using define\_method.

Thanks.

Paolo

---

<div class="post-metadata">

**Author:** ![Paolo\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b9e5f3/32.png) [@Paolo\_N](https://www.ruby-forum.com/u/Paolo_N)\
**Post date:** [October 4, 2006, 2:19am UTC](https://www.ruby-forum.com/t/eval-confusion/70932/2 "2006-10-04T02:19:08Z")

</div>

Hi,

At Wed, 4 Oct 2006 08:00:32 +0900,  
Paolo N. wrote in [ruby-talk:217920]:

> #!/bin/ruby  
> class DefMethod  
> M\_COLLECTION = (‘a’…‘c’)  
> M\_COLLECTION.each do |alpha|  
> define\_method(alpha) {  
> block\_given? ? puts(yield(alpha)) : puts(alpha)

This block\_given? sees the context where define\_method is  
called, that is defining DefMethod and always false.

> ```
> }
> 
> ```
> 
> end  
> end

In 1.9, you have to write as:

> ```
> define_method(alpha) {|&block|
> block ? puts(yield(alpha)) : puts(alpha)
> }
> 
> ```

No way in 1.8.

---

<div class="post-metadata">

**Author:** ![Paolo\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b9e5f3/32.png) [@Paolo\_N](https://www.ruby-forum.com/u/Paolo_N)\
**Post date:** [October 4, 2006, 9:59am UTC](https://www.ruby-forum.com/t/eval-confusion/70932/3 "2006-10-04T09:59:52Z")

</div>

Thank you for replying and for the note on 1.9 very interesting!

Paolo
