From: Joe Van Dyk Date: 2005-12-08T23:54:02+09:00 Subject: Re: do/end vs braces On 12/8/05, James Edward Gray II wrote: > On Dec 8, 2005, at 8:13 AM, Joe Van Dyk wrote: > > > On 12/8/05, 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? > > > > 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 } Joe