From: Mike Gold Date: 2009-01-28T09:21:39+09:00 Subject: Re: Conventions in ruby and the principle of least surprise Ryan Davis wrote: > On Jan 27, 2009, at 02:08 , Damjan Rems wrote: > >> My convention is use {} in oneliners. In all other examples I use >> do..end. > > I follow (and teach) the weirich method: > > http://onestepback.org/index.cgi/Tech/Ruby/BraceVsDoEnd.rdoc > > {} for block values I care about (think map), do/end for regular block > sends (think each). > > I use {} for blocks that return values (think #map) and do/end for all > other blocks (think #each). funcs = [] (1..5).each do |i| funcs << lambda { i } end p funcs.map { |f| f.call } # => [1, 2, 3, 4, 5] funcs2 = [] for j in 1..5 funcs2 << lambda { j } end p funcs2.map { |f| f.call } # => [5, 5, 5, 5, 5] I prefer to make the new binding scope of a block visually obvious, therefore I always use {}. do/end tricks me into thinking it belongs to the same category as for/end or while/end or if/end, but it's quite different (above). Also, I like one rule better than two rules. -- Posted via http://www.ruby-forum.com/.