# Block\_given? problem

**URL:** <https://rubytalk.org/t/block-given-problem/39267>\
**Category:** ruby-talk\
**Created:** [20 July 2007 11:19 UTC](https://rubytalk.org/t/block-given-problem/39267 "2007-07-20T11:19:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Westlin9](https://avatars.discourse-cdn.com/v4/letter/w/ac91a4/32.png) [@Westlin9](https://rubytalk.org/u/Westlin9)\
**Post date:** [20 July 2007 11:19 UTC](https://rubytalk.org/t/block-given-problem/39267/1 "2007-07-20T11:19:58Z")

</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  
&nbsp;&nbsp;def self.TheMethod()  
&nbsp;&nbsp;&nbsp;&nbsp;if block\_given?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "yes, block given"  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "no block is given"  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;'return val'  
&nbsp;&nbsp;end  
end

# ok  
puts TheClass.TheMethod {|param| 123}

# block not detected  
puts TheClass.TheMethod do |param|  
&nbsp;&nbsp;123  
end

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

# ok  
a = TheClass.TheMethod do |param|  
&nbsp;&nbsp;123  
end  
puts a

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [20 July 2007 12:04 UTC](https://rubytalk.org/t/block-given-problem/39267/2 "2007-07-20T12:04:00Z")

</div>

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

> **···**
>
> On 7/20/07, westlin9@gmail.com \<westlin9@gmail.com\> wrote:
> 
> > 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  
> > &nbsp;&nbsp;def self.TheMethod()  
> > &nbsp;&nbsp;&nbsp;&nbsp;if block\_given?  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "yes, block given"  
> > &nbsp;&nbsp;&nbsp;&nbsp;else  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "no block is given"  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;'return val'  
> > &nbsp;&nbsp;end  
> > end
> > 
> > # ok  
> > puts TheClass.TheMethod {|param| 123}
> > 
> > # block not detected  
> > puts TheClass.TheMethod do |param|  
> > &nbsp;&nbsp;123  
> > end
> > 
> > # ok  
> > a = TheClass.TheMethod {|param| 123}  
> > puts a
> > 
> > # ok  
> > a = TheClass.TheMethod do |param|  
> > &nbsp;&nbsp;123  
> > end  
> > puts a
> 
> --  
> I always knew that one day Smalltalk would replace Java.  
> I just didn't know it would be called Ruby  
> -- Kent Beck

---

<div class="post-metadata">

**Author:** ![Mariusz\_Pekala](https://avatars.discourse-cdn.com/v4/letter/m/7ba0ec/32.png) [@Mariusz\_Pekala](https://rubytalk.org/u/Mariusz_Pekala)\
**Post date:** [20 July 2007 12:46 UTC](https://rubytalk.org/t/block-given-problem/39267/3 "2007-07-20T12:46:24Z")

</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  
> &nbsp;&nbsp;def self.TheMethod()  
> &nbsp;&nbsp;&nbsp;&nbsp;if block\_given?  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "yes, block given"  
> &nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "no block is given"  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;'return val'  
> &nbsp;&nbsp;end  
> end
> 
> # ok  
> puts TheClass.TheMethod {|param| 123}
> 
> # block not detected  
> puts TheClass.TheMethod do |param|  
> &nbsp;&nbsp;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|  
> &nbsp;&nbsp;123  
> end  
> puts a

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

> **···**
>
> On 2007-07-20 20:19:58 +0900 (Fri, Jul), westlin9@gmail.com wrote:
> 
> --  
> No virus found in this outgoing message.  
> Checked by 'grep -i virus $MESSAGE'  
> Trust me.

---

<div class="post-metadata">

**Author:** ![Westlin9](https://avatars.discourse-cdn.com/v4/letter/w/ac91a4/32.png) [@Westlin9](https://rubytalk.org/u/Westlin9)\
**Post date:** [20 July 2007 12:20 UTC](https://rubytalk.org/t/block-given-problem/39267/4 "2007-07-20T12:20:01Z")

</div>

Thanks

> **···**
>
> On 20 Juli, 14:04, "Robert Dober" \<robert.do...@gmail.com\> wrote:
> 
> > On 7/20/07, westl...@gmail.com \<westl...@gmail.com\> wrote:
> > 
> > \> 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
> > 
> > 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
> > 
> > --  
> > 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 -

---

<div class="post-metadata">

**Author:** ![Rick\_DeNatale1](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Rick\_DeNatale1](https://rubytalk.org/u/Rick_DeNatale1)\
**Post date:** [20 July 2007 18:13 UTC](https://rubytalk.org/t/block-given-problem/39267/5 "2007-07-20T18:13:37Z")

</div>

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"

> **···**
>
> On 7/20/07, Robert Dober \<robert.dober@gmail.com\> wrote:
> 
> > do...end and {} do not have the same precedence, thus, without the ()  
> > you are doing this  
> > puts( TheClass.TheMethod ) do nil end
> 
> --  
> Rick DeNatale
> 
> My blog on Ruby  
> [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
