# Understanding { } and block\_given

**URL:** <https://www.ruby-forum.com/t/understanding-and-block-given/167477>\
**Category:** Ruby\
**Created:** [May 8, 2009, 7:49am UTC](https://www.ruby-forum.com/t/understanding-and-block-given/167477 "2009-05-08T07:49:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Srijayanth\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e95f7d/32.png) [@Srijayanth\_S](https://www.ruby-forum.com/u/Srijayanth_S)\
**Post date:** [May 8, 2009, 7:49am UTC](https://www.ruby-forum.com/t/understanding-and-block-given/167477/1 "2009-05-08T07:49:53Z")

</div>

Hello,

I have the following:

class DSL  
def DSL.load filename  
dsl = new  
dsl.instance\_eval(File.read(filename))  
dsl  
end  
def to sym,\*args,&block  
if block\_given?  
b=class \<\< self;self;end  
b.instance\_eval {  
define\_method(sym,\*args,&block)  
}  
end  
end  
def method\_missing sym,\*args  
“#{sym}”  
end  
alias :say puts  
end

foo=DSL.load(“sample.dsl”)

sample.dsl:

to smile do  
say “:)”  
end  
to be\_polite {  
say “thank you”  
}  
smile  
be\_polite

The resulting output is:

moonwolf@trantor:~/ruby/dsl/todo$ ./dsl.rb  
🙂  
moonwolf@trantor:~/ruby/dsl/todo$

be\_polite and indeed, anything enclosded in {} doesn’t seem to be  
eval-ed as  
blocks. Why is this?

Jayanth

---

<div class="post-metadata">

**Author:** ![Srijayanth\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e95f7d/32.png) [@Srijayanth\_S](https://www.ruby-forum.com/u/Srijayanth_S)\
**Post date:** [May 8, 2009, 11:42am UTC](https://www.ruby-forum.com/t/understanding-and-block-given/167477/2 "2009-05-08T11:42:37Z")

</div>

This is definitely not a block\_given thing.

block is coming through as a NilClass it seems.

puts block.class yields NilClass. The strange bit is that when I do it  
outside of instance\_eval with just a define\_method under irb, it works  
as  
expected. I am lost.

Anyone?

Jayanth

---

<div class="post-metadata">

**Author:** ![Srijayanth\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e95f7d/32.png) [@Srijayanth\_S](https://www.ruby-forum.com/u/Srijayanth_S)\
**Post date:** [May 8, 2009, 11:56am UTC](https://www.ruby-forum.com/t/understanding-and-block-given/167477/3 "2009-05-08T11:56:05Z")

</div>

Le 08 mai à 11:42, Srijayanth S. a écrit :

> [Note: parts of this message were removed to make it a legal post.]
> 
> This is definitely not a block\_given thing.

It’s an operator precedence thing. {} binds at a higher precedence than  
do/end, meaning that, in the absence of parentheses, in one case, it’s  
the first function that takes the block and in the other, it’s the  
second one.

For instance :

> > def test ; (block\_given? ? ‘block’ : ‘no block’) ; end  
> > =\> nil
> 
> > puts test do end  
> > no block  
> > =\> nil
> 
> > puts test { }  
> > block

In the first case, it’s puts who gets the block (and discards it). In  
the second case, it’s my function. Compare to :

> > puts(test) do end  
> > no block  
> > =\> nil
> 
> > puts(test) {}  
> > no block  
> > =\> nil
> 
> > puts(test do end)  
> > block  
> > =\> nil
> 
> > puts(test {})  
> > block

Fred
