# Block\_given? problem

**URL:** <https://www.ruby-forum.com/t/block-given-problem/109938>\
**Category:** Ruby\
**Created:** [July 20, 2007, 1:21pm UTC](https://www.ruby-forum.com/t/block-given-problem/109938 "2007-07-20T13:21:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 20, 2007, 1:21pm UTC](https://www.ruby-forum.com/t/block-given-problem/109938/1 "2007-07-20T13:21:02Z")

</div>

I can’t figure out why the do…end block isn’t detected by the  
“block\_given?” function. The problem occurs when a method call with a  
block is used in a puts statement. See the code example below.  
Can some one explain this to me please? Is this a bug?

class TheClass  
def self.TheMethod()  
if block\_given?  
puts “yes, block given”  
else  
puts “no block is given”  
end  
‘return val’  
end  
end

# ok

puts TheClass.TheMethod {|param| 123}

# block not detected

puts TheClass.TheMethod do |param|  
123  
end

# ok

a = TheClass.TheMethod {|param| 123}  
puts a

# ok

a = TheClass.TheMethod do |param|  
123  
end  
puts a

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 20, 2007, 2:05pm UTC](https://www.ruby-forum.com/t/block-given-problem/109938/2 "2007-07-20T14:05:07Z")

</div>

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

> ```
> else
> 
> ```
> 
> puts TheClass.TheMethod do |param|  
> end  
> puts a

puts( TheClass.TheMethod do nil end )

do…end and {} do not have the same precedence, thus, without the ()  
you are doing this  
puts( TheClass.TheMethod ) do nil end

HTH  
Robert

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 20, 2007, 2:22pm UTC](https://www.ruby-forum.com/t/block-given-problem/109938/3 "2007-07-20T14:22:10Z")

</div>

On 20 Juli, 14:04, “Robert D.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > def self.TheMethod()  
> > puts TheClass.TheMethod {|param| 123}
> > 
> > # ok
> 
> HTH  
> Robert
> 
> –  
> I always knew that one day Smalltalk would replace Java.  
> I just didn’t know it would be called Ruby  
> – Kent Beck- Dölj citerad text -
> 
> - Visa citerad text -

Thanks

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 20, 2007, 2:47pm UTC](https://www.ruby-forum.com/t/block-given-problem/109938/4 "2007-07-20T14:47:12Z")

</div>

On 2007-07-20 20:19:58 +0900 (Fri, Jul), removed\_email\_address@domain.invalid wrote:

> ```
> else
> 
> ```
> 
> puts TheClass.TheMethod do |param|  
> 123  
> end

This is because do…end block gets attached to the ‘puts’ instead of  
TheMethod.

do…end and {||…} differ in precedence. {||…} binds to the rightmost  
method, while do…end to the leftmost one.

> # ok
> 
> a = TheClass.TheMethod {|param| 123}  
> puts a
> 
> # ok
> 
> a = TheClass.TheMethod do |param|  
> 123  
> end  
> puts a

There you have no ambiguity as there is only one method call.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 20, 2007, 8:14pm UTC](https://www.ruby-forum.com/t/block-given-problem/109938/5 "2007-07-20T20:14:54Z")

</div>

On 7/20/07, Robert D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> do…end and {} do not have the same precedence, thus, without the ()  
> you are doing this  
> puts( TheClass.TheMethod ) do nil end

do…end vs {}  
and vs. &&  
or vs. ||  
not vs !

I think that it can be said that generally in ruby “symbols bind  
tighter than words”

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
