# Fwd: how to do "do" blocks

**URL:** <https://www.ruby-forum.com/t/fwd-how-to-do-do-blocks/115777>\
**Category:** Ruby\
**Created:** [September 28, 2007, 12:54pm UTC](https://www.ruby-forum.com/t/fwd-how-to-do-do-blocks/115777 "2007-09-28T12:54:41Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![sijomo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/87869e/32.png) [@sijomo](https://www.ruby-forum.com/u/sijomo)\
**Post date:** [September 28, 2007, 12:54pm UTC](https://www.ruby-forum.com/t/fwd-how-to-do-do-blocks/115777/1 "2007-09-28T12:54:41Z")

</div>

I posted this earlier, but it didn’t get through… (see my other post  
“Ac Cautionary Tale”).

hehe

SM

---------- Forwarded message ----------  
From: Simon M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Date: Sep 28, 2007 10:32 AM  
Subject: Re: how to do “do” blocks  
To: removed\_email\_address@domain.invalid

Hey

The “do…end” and “{…}” are almost equivalent (although “{…}” has  
higher precedence).

So,

collection\_of\_objects = []  
10.times do  
collection\_of\_objects \<\< MyObject.create  
end

Is the equivalent to:

collection\_of\_objects = []  
10.times {  
collection\_of\_objects \<\< MyObject.create  
}

But (from the Ruby Cookbook), you may need to look at precedence.

So,

1.upto 3 do |x|  
puts x  
end

is ok.

But,

1.upto 3 { |x|  
puts x  
end

is not, as the code block binds to the 3, not the function call.

So, it would have to be:

1.upto(3) { |x|  
puts x  
end

Hope this helps!

SM

On 9/28/07, Daniel T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 10.times do { collection\_of\_objects \<\< MyObject.create }
> 
> ## I didn’t understand there was ANY difference between the two syntaxes really. Can someone explain to me the finer points?
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

–  
Simon M.
