From: Gennady Bystritsky Date: 2004-05-02T09:43:20+09:00 Subject: Re: Ruby Newbie (ooh, that rhymes!) - When do I use do/end, when {}? On May 1, 2004, at 2:33 PM, Mark Hubbart wrote: > > On May 1, 2004, at 1:54 PM, Glenn wrote: > >> I've read the User Guide and other docs on the net, but I'm a little >> unsure as to whether I can ALWAYS substitute the DO/END with curly >> brackets for a block? For example, I've only ever seen referenced to >> hash definitions using curlies. What's the rule here? Is it purely >> down to preference? As somebody has mentioned already it is not only preference, but also precedence rules ({} sticks better than do...end, meaning higher precedence). > > While there is some amount of preference involved, there is a general > rule that most Ruby programmers (and matz, I believe) agree on: > brackets for one-line blocks, do...end for longer blocks. ie: > > 5.times {|n| puts n } There's also another usage some people, including myself, agree on: do...end for blocks that actually DO stuff, like in Array#each, File.open, Integer#times (Mark, sorry for contradiction ;-)), etc.; {} for blocks that define conditions, like in map, select/reject, sort, etc. Sincerely, Gennady Bystritsky