# Do/end vs braces

**URL:** <https://rubytalk.org/t/do-end-vs-braces/23116>\
**Category:** ruby-talk\
**Created:** [8 December 2005 14:09 UTC](https://rubytalk.org/t/do-end-vs-braces/23116 "2005-12-08T14:09:06Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steve\_Litt](https://avatars.discourse-cdn.com/v4/letter/s/3ec8ea/32.png) [@Steve\_Litt](https://rubytalk.org/u/Steve_Litt)\
**Post date:** [8 December 2005 14:09 UTC](https://rubytalk.org/t/do-end-vs-braces/23116/1 "2005-12-08T14:09:06Z")

</div>

Hi all,

It looks to me like when you use an iterator (each for instance), you can make  
the block either from a do/end pair, or from curly braces. All things being  
equal, I'd greatly prefer to use do/end. Are there any differences in runtime  
speed, capabilities, or Rubyness?

#!/usr/bin/ruby  
(1..4).each do |i|  
&nbsp;&nbsp;puts i  
end  
print "================\n"  
(1..4).each { |i|  
&nbsp;&nbsp;puts i  
}

Output:

[slitt@mydesk slitt]$ ./test.rb  
1  
2  
3  
4

> **···**
>
> ================  
> 1  
> 2  
> 3  
> 4  
> [slitt@mydesk slitt]$
> 
> Thanks
> 
> SteveT
> 
> Steve Litt  
> [http://www.troubleshooters.com](http://www.troubleshooters.com)  
> [slitt@troubleshooters.com](mailto:slitt@troubleshooters.com)

---

<div class="post-metadata">

**Author:** ![J-Van](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@J-Van](https://rubytalk.org/u/J-Van)\
**Post date:** [8 December 2005 14:13 UTC](https://rubytalk.org/t/do-end-vs-braces/23116/2 "2005-12-08T14:13:30Z")

</div>

Typically,

5.times { |i| puts i }

5.times do |i|  
&nbsp;&nbsp;puts i  
end

Generally people save do .. end for multiline stuff. Don't think  
there's a difference in speed.

Sometimes people who use vim use only braces for vim's matching  
capabilities, although didn't someone fix that for do..end?

> **···**
>
> On 12/8/05, Steve Litt \<slitt@earthlink.net\> wrote:
> 
> > Hi all,
> > 
> > It looks to me like when you use an iterator (each for instance), you can make  
> > the block either from a do/end pair, or from curly braces. All things being  
> > equal, I'd greatly prefer to use do/end. Are there any differences in runtime  
> > speed, capabilities, or Rubyness?

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [8 December 2005 14:51 UTC](https://rubytalk.org/t/do-end-vs-braces/23116/3 "2005-12-08T14:51:57Z")

</div>

There is a subtle difference in binding precedence. I wrote about that in this message:

[http://groups.google.com/group/comp.lang.ruby/msg/b67b116e49473e11](http://groups.google.com/group/comp.lang.ruby/msg/b67b116e49473e11)

Hope that helps.

James Edward Gray II

> **···**
>
> On Dec 8, 2005, at 8:09 AM, Steve Litt wrote:
> 
> > Are there any differences in runtime speed, capabilities, or Rubyness?

---

<div class="post-metadata">

**Author:** ![Daniel\_Schierbeck](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@Daniel\_Schierbeck](https://rubytalk.org/u/Daniel_Schierbeck)\
**Post date:** [8 December 2005 15:07 UTC](https://rubytalk.org/t/do-end-vs-braces/23116/4 "2005-12-08T15:07:36Z")

</div>

Steve Litt wrote:

> Hi all,
> 
> It looks to me like when you use an iterator (each for instance), you can make the block either from a do/end pair, or from curly braces. All things being equal, I'd greatly prefer to use do/end. Are there any differences in runtime speed, capabilities, or Rubyness?

There's a slight difference in how they're parsed.

&nbsp;&nbsp;&nbsp;foo bar { block }

is the same as

&nbsp;&nbsp;&nbsp;foo(bar { block })

while

&nbsp;&nbsp;&nbsp;foo bar do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;block  
&nbsp;&nbsp;&nbsp;end

is the same as

&nbsp;&nbsp;&nbsp;foo(bar) do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;block  
&nbsp;&nbsp;&nbsp;end

or

&nbsp;&nbsp;&nbsp;foo(bar) { block }

but it's only an issue when you leave out the parantheses. The styling convention is that the curly braces are used for one-line blocks and the do..end for multi-line blocks

&nbsp;&nbsp;&nbsp;foo { make\_me\_a\_bar }  
&nbsp;&nbsp;&nbsp;bar do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foo = Foo.new  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pass\_the\_foo(foo)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;do\_something\_else  
&nbsp;&nbsp;&nbsp;end

Cheers,  
Daniel

---

<div class="post-metadata">

**Author:** ![MenTaLguY](https://avatars.discourse-cdn.com/v4/letter/m/439d5e/32.png) [@MenTaLguY](https://rubytalk.org/u/MenTaLguY)\
**Post date:** [8 December 2005 15:27 UTC](https://rubytalk.org/t/do-end-vs-braces/23116/5 "2005-12-08T15:27:13Z")

</div>

Quoting Joe Van Dyk [joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org](mailto:joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org):

> 5.times { |i| puts i }
> 
> 5.times do |i|  
> puts i  
> end

> Generally people save do … end for multiline stuff. Don’t think  
> there’s a difference in speed.

I can promise there’s no speed difference. They both parse to the  
same thing:

(iter  
(call  
(lit #\<5\>) times)  
(dasgn\_curr i)  
(fcall puts  
(array  
(dvar i))))

(iter  
(call  
(lit #\<5\>) times)  
(dasgn\_curr i)  
(fcall puts  
(array  
(dvar i))))

However, {} and do…end are not totally interchangable as syntax.  
These are all equivalent:

obj.meth( 1, 2 ) { |i|  
puts i  
}

obj.meth( 1, 2 ) do |i|  
puts i  
end

obj.meth 1, 2 do |i|  
puts i  
end

But this will net you a parse error (‘2’ is not a valid method  
name):

obj.meth 1, 2 { |i|  
puts i  
}

And these two parse equivalently:

obj.meth 1, zog { |i|  
puts i  
}

obj.meth( 1, zog { |i| puts i } )

Think of {} as being more “clingy” than do…end.

Basically, {} has higher precedence than do…end, just as (among  
otherwise equivalent operators) && has higher precedence than  
‘and’, and || has higher precedence than ‘or’.

I’m sure Ter hates Ruby now. 🙂

-mental

---

<div class="post-metadata">

**Author:** ![Terence\_Parr](https://avatars.discourse-cdn.com/v4/letter/t/4da419/32.png) [@Terence\_Parr](https://rubytalk.org/u/Terence_Parr)\
**Post date:** [8 December 2005 22:44 UTC](https://rubytalk.org/t/do-end-vs-braces/23116/6 "2005-12-08T22:44:41Z")

</div>

> But this will net you a parse error (‘2’ is not a valid method  
> name):
> 
> obj.meth 1, 2 { |i|  
> puts i  
> }

Yikes! Not sure what’s up there. What does the {…} bind to then?  
The 2? If so, why is 2 considered a method.

> And these two parse equivalently:
> 
> obj.meth 1, zog { |i|  
> puts i  
> }
> 
> obj.meth( 1, zog { |i| puts i } )
> 
> Think of {} as being more “clingy” than do…end.

That I can parse mentally. 🙂

> Basically, {} has higher precedence than do…end, just as (among  
> otherwise equivalent operators) && has higher precedence than  
> ‘and’, and || has higher precedence than ‘or’.
> 
> I’m sure Ter hates Ruby now. 🙂

Well, I am trying to ignore edge cases while I learn the language 😉  
Starting with the warts is probably not a good idea 😉

Ter

> **···**
>
> On Dec 8, 2005, at 7:27 AM, [mental-uFjFfPRxV21eoWH0uzbU5w@public.gmane.org](mailto:mental-uFjFfPRxV21eoWH0uzbU5w@public.gmane.org) wrote:

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [8 December 2005 14:47 UTC](https://rubytalk.org/t/do-end-vs-braces/23116/7 "2005-12-08T14:47:02Z")

</div>

Another convention some use is that { ... } are for the times when you care about the return value and do ... end is for when you're interested in the side effects. For example:

sum = [1, 2, 3].inject { |sum, n| sum + n } # return value

File.open("total.txt", "w") do |file| # side effects  
&nbsp;&nbsp;&nbsp;file.puts sum  
end

James Edward Gray II

> **···**
>
> On Dec 8, 2005, at 8:13 AM, Joe Van Dyk wrote:
> 
> > On 12/8/05, Steve Litt \<slitt@earthlink.net\> wrote:
> > 
> > > Hi all,
> > > 
> > > It looks to me like when you use an iterator (each for instance), you can make  
> > > the block either from a do/end pair, or from curly braces. All things being  
> > > equal, I'd greatly prefer to use do/end. Are there any differences in runtime  
> > > speed, capabilities, or Rubyness?
> > 
> > Typically,
> > 
> > 5.times { |i| puts i }
> > 
> > 5.times do |i|  
> > &nbsp;&nbsp;puts i  
> > end
> > 
> > Generally people save do .. end for multiline stuff. Don't think  
> > there's a difference in speed.

---

<div class="post-metadata">

**Author:** ![MenTaLguY](https://avatars.discourse-cdn.com/v4/letter/m/439d5e/32.png) [@MenTaLguY](https://rubytalk.org/u/MenTaLguY)\
**Post date:** [9 December 2005 06:33 UTC](https://rubytalk.org/t/do-end-vs-braces/23116/8 "2005-12-09T06:33:30Z")

</div>

Binds to the 2, yes. But then it fails to match, as 2 isn’t a method.  
It should also fail if there were an lvar rather than a private method  
call in that position.

That’s an interesting question actually … how it matches enough to  
bind, but not enough to succeed.

-mental

> **···**
>
> On Thu, 2005-12-08 at 14:44 -0800, Terence Parr wrote:
> 
> > On Dec 8, 2005, at 7:27 AM, [mental-uFjFfPRxV21eoWH0uzbU5w@public.gmane.org](mailto:mental-uFjFfPRxV21eoWH0uzbU5w@public.gmane.org) wrote:
> > 
> > > But this will net you a parse error (‘2’ is not a valid method  
> > > name):
> > > 
> > > obj.meth 1, 2 { |i|  
> > > puts i  
> > > }
> > 
> > Yikes! Not sure what’s up there. What does the {…} bind to then?  
> > The 2? If so, why is 2 considered a method.

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello1](https://avatars.discourse-cdn.com/v4/letter/m/3be4f8/32.png) [@Martin\_DeMello1](https://rubytalk.org/u/Martin_DeMello1)\
**Post date:** [8 December 2005 17:27 UTC](https://rubytalk.org/t/do-end-vs-braces/23116/9 "2005-12-08T17:27:36Z")

</div>

matchit.vim

martin

> **···**
>
> Joe Van Dyk \<joevandyk@gmail.com\> wrote:
> 
> > Sometimes people who use vim use only braces for vim's matching  
> > capabilities, although didn't someone fix that for do..end?

---

<div class="post-metadata">

**Author:** ![Steve\_Litt](https://avatars.discourse-cdn.com/v4/letter/s/3ec8ea/32.png) [@Steve\_Litt](https://rubytalk.org/u/Steve_Litt)\
**Post date:** [8 December 2005 22:36 UTC](https://rubytalk.org/t/do-end-vs-braces/23116/10 "2005-12-08T22:36:51Z")

</div>

> Steve Litt wrote:  
> \> Hi all,  
> \>  
> \> It looks to me like when you use an iterator (each for instance), you can  
> \> make the block either from a do/end pair, or from curly braces. All  
> \> things being equal, I'd greatly prefer to use do/end. Are there any  
> \> differences in runtime speed, capabilities, or Rubyness?
> 
> There's a slight difference in how they're parsed.
> 
> &nbsp;&nbsp;&nbsp;foo bar { block }
> 
> is the same as
> 
> &nbsp;&nbsp;&nbsp;foo(bar { block })
> 
> while
> 
> &nbsp;&nbsp;&nbsp;foo bar do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;block  
> &nbsp;&nbsp;&nbsp;end
> 
> is the same as
> 
> &nbsp;&nbsp;&nbsp;foo(bar) do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;block  
> &nbsp;&nbsp;&nbsp;end
> 
> or
> 
> &nbsp;&nbsp;&nbsp;foo(bar) { block }
> 
> but it's only an issue when you leave out the parantheses.

Except I can't put in the parentheses:

#!/usr/bin/ruby  
my\_array = ["alpha", "beta", "gamma"]  
puts (my\_array.collect do  
&nbsp;&nbsp;\>word\>  
&nbsp;&nbsp;word.capitalize  
end)

Output:  
[slitt@mydesk slitt]$ ./test.rb  
./test.rb:3: syntax error  
./test.rb:6: syntax error  
[slitt@mydesk slitt]$

Thanks

SteveT

> **···**
>
> On Thursday 08 December 2005 10:07 am, Daniel Schierbeck wrote:
> 
> > The styling  
> > convention is that the curly braces are used for one-line blocks and the  
> > do..end for multi-line blocks
> > 
> > &nbsp;&nbsp;&nbsp;foo { make\_me\_a\_bar }  
> > &nbsp;&nbsp;&nbsp;bar do  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foo = Foo.new  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pass\_the\_foo(foo)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;do\_something\_else  
> > &nbsp;&nbsp;&nbsp;end
> > 
> > Cheers,  
> > Daniel
> 
> --  
> Steve Litt
> 
> > **[Troubleshooters.Com](http://www.troubleshooters.com)**
> >
> > The process and mindset of troubleshooting.
> 
> slitt@troubleshooters.com

---

<div class="post-metadata">

**Author:** ![Yohanes\_Santoso1](https://avatars.discourse-cdn.com/v4/letter/y/ccd318/32.png) [@Yohanes\_Santoso1](https://rubytalk.org/u/Yohanes_Santoso1)\
**Post date:** [9 December 2005 14:59 UTC](https://rubytalk.org/t/do-end-vs-braces/23116/11 "2005-12-09T14:59:36Z")

</div>

Joe Van Dyk \<joevandyk@gmail.com\> writes:

> Generally people save do .. end for multiline stuff. Don't think

I am not sure why Pickaxe mentioned this convention that is based  
on LOC factor rather than one that is based on intent.

Basing it on LOC is silly, IMO. It really is more suitable for  
languages that are white-space sensitive.

Consider you wrote:

foo.bar {|a| zoo(a)}

Oh, further down the road you realise that it is valuable to log the  
value of a or perhaps you are simply reformatting your code.

foo.bar {|a|  
&nbsp;&nbsp;log.debug(a)  
&nbsp;&nbsp;zoo(a)  
}

Whoops, that's out of the convention. Your eyes are itchy at that, so  
you spent some time fixing it like:

foo.bar do |a|  
&nbsp;&nbsp;&nbsp;log.debug(a)  
&nbsp;&nbsp;&nbsp;zoo(a)  
end

Later on, you decided that logging the value of a is frivolous, so you  
changed it again:

foo.bar do |a| zoo(a) end

That violates the convention as well, so you do yet another do-end to  
{} transformation

foo.bar {|a| zoo(a)}

All that works is just for one method call. Imagine if there are more,  
and there are likely to be more.

Basing it on intent (return value or side-effect or what-have-you)  
seems a more rational guideline. For certain, that would make code  
reformatting a more pleasurable exercise.

YS.

---

<div class="post-metadata">

**Author:** ![Neil\_Stevens](https://avatars.discourse-cdn.com/v4/letter/n/e9c0ed/32.png) [@Neil\_Stevens](https://rubytalk.org/u/Neil_Stevens)\
**Post date:** [9 December 2005 17:07 UTC](https://rubytalk.org/t/do-end-vs-braces/23116/12 "2005-12-09T17:07:36Z")

</div>

Joe Van Dyk wrote:

> Generally people save do .. end for multiline stuff. Don't think  
> there's a difference in speed.

I use do .. end always. I find it easier to read, and more in line with  
the other foo .. end structures in ruby.

> **···**
>
> --  
> Neil Stevens - neil@hakubi.us
> 
> 'A republic, if you can keep it.' -- Benjamin Franklin

---

<div class="post-metadata">

**Author:** ![J-Van](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@J-Van](https://rubytalk.org/u/J-Van)\
**Post date:** [8 December 2005 14:54 UTC](https://rubytalk.org/t/do-end-vs-braces/23116/13 "2005-12-08T14:54:02Z")

</div>

Good point. And also use { ... } if you want to do crazy chains like

[1, 2, 3].collect { |a| i \* a }.each { |a| puts a }

Joe

> **···**
>
> On 12/8/05, James Edward Gray II \<james@grayproductions.net\> wrote:
> 
> > On Dec 8, 2005, at 8:13 AM, Joe Van Dyk wrote:
> > 
> > \> On 12/8/05, Steve Litt \<slitt@earthlink.net\> wrote:  
> > \>\> Hi all,  
> > \>\>  
> > \>\> It looks to me like when you use an iterator (each for instance),  
> > \>\> you can make  
> > \>\> the block either from a do/end pair, or from curly braces. All  
> > \>\> things being  
> > \>\> equal, I'd greatly prefer to use do/end. Are there any differences  
> > \>\> in runtime  
> > \>\> speed, capabilities, or Rubyness?  
> > \>  
> > \> Typically,  
> > \>  
> > \> 5.times { |i| puts i }  
> > \>  
> > \> 5.times do |i|  
> > \> puts i  
> > \> end  
> > \>  
> > \> Generally people save do .. end for multiline stuff. Don't think  
> > \> there's a difference in speed.
> > 
> > Another convention some use is that { ... } are for the times when  
> > you care about the return value and do ... end is for when you're  
> > interested in the side effects. For example:
> > 
> > sum = [1, 2, 3].inject { |sum, n| sum + n } # return value
> > 
> > File.open("total.txt", "w") do |file| # side effects  
> > &nbsp;&nbsp;&nbsp;file.puts sum  
> > end

---

<div class="post-metadata">

**Author:** ![Jacob\_Fugal](https://avatars.discourse-cdn.com/v4/letter/j/439d5e/32.png) [@Jacob\_Fugal](https://rubytalk.org/u/Jacob_Fugal)\
**Post date:** [8 December 2005 23:06 UTC](https://rubytalk.org/t/do-end-vs-braces/23116/14 "2005-12-08T23:06:08Z")

</div>

> Except I can't put in the parentheses:
> 
> #!/usr/bin/ruby  
> my\_array = ["alpha", "beta", "gamma"]  
> puts (my\_array.collect do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\>word\>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;word.capitalize  
> end)
> 
> Output:  
> [slitt@mydesk slitt]$ ./test.rb  
> ./test.rb:3: syntax error  
> ./test.rb:6: syntax error

Take out the space between puts and the open paren:

~$ cat test.rb  
my\_array = ["alpha", "beta", "gamma"]  
puts(my\_array.collect do  
&nbsp;&nbsp;\>word\>  
&nbsp;&nbsp;word.capitalize  
end)

~$ ruby test.rb  
Alpha  
Beta  
Gamma

Jacob Fugal

> **···**
>
> On 12/8/05, Steve Litt \<slitt@earthlink.net\> wrote:

---

<div class="post-metadata">

**Author:** ![Daniel\_Cedilotte](https://avatars.discourse-cdn.com/v4/letter/d/c0e974/32.png) [@Daniel\_Cedilotte](https://rubytalk.org/u/Daniel_Cedilotte)\
**Post date:** [9 December 2005 15:21 UTC](https://rubytalk.org/t/do-end-vs-braces/23116/15 "2005-12-09T15:21:41Z")

</div>

Yohanes Santoso wrote:

> Joe Van Dyk \<joevandyk@gmail.com\> writes:
> 
> > Generally people save do .. end for multiline stuff. Don't think  
> > &nbsp;&nbsp;&nbsp;  
> > I am not sure why Pickaxe mentioned this convention that is based  
> > on LOC factor rather than one that is based on intent.
> 
> Basing it on LOC is silly, IMO. It really is more suitable for  
> languages that are white-space sensitive.
> 
> Consider you wrote:
> 
> foo.bar {|a| zoo(a)}
> 
> Oh, further down the road you realise that it is valuable to log the  
> value of a or perhaps you are simply reformatting your code.
> 
> foo.bar {|a|  
> log.debug(a)  
> zoo(a)  
> }
> 
> Whoops, that's out of the convention. Your eyes are itchy at that, so  
> you spent some time fixing it like:
> 
> foo.bar do |a|  
> &nbsp;&nbsp;log.debug(a)  
> &nbsp;&nbsp;zoo(a)  
> end
> 
> Later on, you decided that logging the value of a is frivolous, so you  
> changed it again:
> 
> foo.bar do |a| zoo(a) end
> 
> That violates the convention as well, so you do yet another do-end to  
> {} transformation
> 
> foo.bar {|a| zoo(a)}
> 
> All that works is just for one method call. Imagine if there are more,  
> and there are likely to be more.
> 
> Basing it on intent (return value or side-effect or what-have-you)  
> seems a more rational guideline. For certain, that would make code  
> reformatting a more pleasurable exercise.
> 
> YS.

In the end doesn't it all come up to a matter of preference? I prefer the curly braces, others prefer do..end. In my book its a matter of versatility of the language. Which in turn is a verry nice feature, IMHO.

Daniel C.

---

<div class="post-metadata">

**Author:** ![Ezra\_Zygmuntowicz4](https://avatars.discourse-cdn.com/v4/letter/e/d2c977/32.png) [@Ezra\_Zygmuntowicz4](https://rubytalk.org/u/Ezra_Zygmuntowicz4)\
**Post date:** [9 December 2005 18:24 UTC](https://rubytalk.org/t/do-end-vs-braces/23116/16 "2005-12-09T18:24:10Z")

</div>

Yohanes-

&nbsp;&nbsp;I have been using this link[1] for converting between do .. end and {..} in vim and it works great. Thought I would share.

Cheers-  
-Ezra Zygmuntowicz  
Yakima Herald-Republic  
WebMaster

> **[yakimaherald.com | Yakima, Washington - We Tell Your Stories](https://www.yakimaherald.com/)**

509-577-7732  
ezra@yakima-herald.com

[1] [http://eigenclass.org/hiki.rb?Ruby+block+conversion+macros+for+Vim](http://eigenclass.org/hiki.rb?Ruby+block+conversion+macros+for+Vim)

> **···**
>
> On Dec 9, 2005, at 6:59 AM, Yohanes Santoso wrote:
> 
> > Joe Van Dyk \<joevandyk@gmail.com\> writes:
> > 
> > > Generally people save do .. end for multiline stuff. Don't think

---

<div class="post-metadata">

**Author:** ![J-Van](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@J-Van](https://rubytalk.org/u/J-Van)\
**Post date:** [8 December 2005 14:54 UTC](https://rubytalk.org/t/do-end-vs-braces/23116/17 "2005-12-08T14:54:39Z")

</div>

Oops, that first 'i' should be an 'a'.

> **···**
>
> On 12/8/05, Joe Van Dyk \<joevandyk@gmail.com\> wrote:
> 
> > On 12/8/05, James Edward Gray II \<james@grayproductions.net\> wrote:  
> > \> On Dec 8, 2005, at 8:13 AM, Joe Van Dyk wrote:  
> > \>  
> > \> \> On 12/8/05, Steve Litt \<slitt@earthlink.net\> wrote:  
> > \> \>\> Hi all,  
> > \> \>\>  
> > \> \>\> It looks to me like when you use an iterator (each for instance),  
> > \> \>\> you can make  
> > \> \>\> the block either from a do/end pair, or from curly braces. All  
> > \> \>\> things being  
> > \> \>\> equal, I'd greatly prefer to use do/end. Are there any differences  
> > \> \>\> in runtime  
> > \> \>\> speed, capabilities, or Rubyness?  
> > \> \>  
> > \> \> Typically,  
> > \> \>  
> > \> \> 5.times { |i| puts i }  
> > \> \>  
> > \> \> 5.times do |i|  
> > \> \> puts i  
> > \> \> end  
> > \> \>  
> > \> \> Generally people save do .. end for multiline stuff. Don't think  
> > \> \> there's a difference in speed.  
> > \>  
> > \> Another convention some use is that { ... } are for the times when  
> > \> you care about the return value and do ... end is for when you're  
> > \> interested in the side effects. For example:  
> > \>  
> > \> sum = [1, 2, 3].inject { |sum, n| sum + n } # return value  
> > \>  
> > \> File.open("total.txt", "w") do |file| # side effects  
> > \> file.puts sum  
> > \> end
> > 
> > Good point. And also use { ... } if you want to do crazy chains like
> > 
> > [1, 2, 3].collect { |a| i \* a }.each { |a| puts a }

---

<div class="post-metadata">

**Author:** ![Steve\_Litt](https://avatars.discourse-cdn.com/v4/letter/s/3ec8ea/32.png) [@Steve\_Litt](https://rubytalk.org/u/Steve_Litt)\
**Post date:** [9 December 2005 01:11 UTC](https://rubytalk.org/t/do-end-vs-braces/23116/18 "2005-12-09T01:11:29Z")

</div>

Confirmed! Thanks Jacob. This is the first time I've seen Ruby conflict with  
the Rule of Least Surprise ([http://www.faqs.org/docs/artu/ch11s01.html\](http://www.faqs.org/docs/artu/ch11s01.html%5C)).

Is there any chance of a Ruby change so as not to require the paren to be  
flush up against the function name?

Thanks

SteveT

Steve Litt

> **[Troubleshooters.Com](http://www.troubleshooters.com)**
>
> The process and mindset of troubleshooting.

slitt@troubleshooters.com

> **···**
>
> On Thursday 08 December 2005 06:06 pm, Jacob Fugal wrote:
> 
> > On 12/8/05, Steve Litt \<slitt@earthlink.net\> wrote:  
> > \> Except I can't put in the parentheses:  
> > \>  
> > \> #!/usr/bin/ruby  
> > \> my\_array = ["alpha", "beta", "gamma"]  
> > \> puts (my\_array.collect do  
> > \>  
> > \> \>word\>  
> > \>  
> > \> word.capitalize  
> > \> end)  
> > \>  
> > \> Output:  
> > \> [slitt@mydesk slitt]$ ./test.rb  
> > \> ./test.rb:3: syntax error  
> > \> ./test.rb:6: syntax error
> > 
> > Take out the space between puts and the open paren:

---

<div class="post-metadata">

**Author:** ![Gary\_Wright](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@Gary\_Wright](https://rubytalk.org/u/Gary_Wright)\
**Post date:** [9 December 2005 16:35 UTC](https://rubytalk.org/t/do-end-vs-braces/23116/19 "2005-12-09T16:35:06Z")

</div>

I find the different precedence useful.

If I have a method call written without parens, which is common  
in 'imperative style' ruby, it is very useful to have the do/block  
not bind to the last argument. Such as rake tasks:

task :test do  
&nbsp;&nbsp;&nbsp;ruby "test/unittest.rb"  
end

> **···**
>
> On Dec 9, 2005, at 10:21 AM, Daniel Cedilotte wrote:
> 
> > In the end doesn't it all come up to a matter of preference? I prefer the curly braces, others prefer do..end. In my book its a matter of versatility of the language. Which in turn is a verry nice feature, IMHO.

---

<div class="post-metadata">

**Author:** ![Yohanes\_Santoso1](https://avatars.discourse-cdn.com/v4/letter/y/ccd318/32.png) [@Yohanes\_Santoso1](https://rubytalk.org/u/Yohanes_Santoso1)\
**Post date:** [9 December 2005 19:45 UTC](https://rubytalk.org/t/do-end-vs-braces/23116/20 "2005-12-09T19:45:07Z")

</div>

Ezra Zygmuntowicz \<ezra@yakimaherald.com\> writes:

> Yohanes-
> 
> &nbsp;&nbsp;I have been using this link[1] for converting between do  
> .. end and {..} in vim and it works great. Thought I would share.

Thank you for the thought, although I can't use that as I'm using  
emacs, not vim. I also have a similar function that does that in  
emacs, but that's beside my point.

My point is, that Pickaxe is recommending a convention that compels  
the programmer to do something when the LOC changes, unexpected.

They could have recommended a convention that is not based on LOC.

YS.

[Next page](https://rubytalk.org/t/do-end-vs-braces/23116.md?page=2)
