From: Steve Litt Date: 2005-12-09T07:36:51+09:00 Subject: Re: do/end vs braces On Thursday 08 December 2005 10:07 am, Daniel Schierbeck wrote: > 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. > > foo bar { block } > > is the same as > > foo(bar { block }) > > while > > foo bar do > block > end > > is the same as > > foo(bar) do > block > end > > or > > 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 |word| word.capitalize end) Output: [slitt@mydesk slitt]$ ./test.rb ./test.rb:3: syntax error ./test.rb:6: syntax error [slitt@mydesk slitt]$ Thanks SteveT > The styling > convention is that the curly braces are used for one-line blocks and the > do..end for multi-line blocks > > foo { make_me_a_bar } > bar do > foo = Foo.new > pass_the_foo(foo) > do_something_else > end > > > Cheers, > Daniel -- Steve Litt http://www.troubleshooters.com slitt@troubleshooters.com