From: Eric Mahurin Date: 2005-05-12T21:51:03+09:00 Subject: Re: simple object loops and what they return > > > That sure looks ugly. I don't see any advantage of this > over: > > > > > > my_array.each do |item| > > > puts "item" > > > end > > > if my_array.empty? > > > puts "(no items found)" > > > end > > > > *shrug* I personally prefer what I posted over this. (I > tend to use { > > and } for blocks instead of begin/end in my own code > because IMHO it > > visually groups it better, but I used begin/end here since > that seems > > to be the emerging multi-line choice of the community.) > > [snip] > > I just wanted to throw in that I'm in the camp of people who > use begin > end when the side effect is important and braces when the > return value > is important. This conveys more meaning on a causual glance. > I.e. > > print "100!" > puts (1..100).inject(1) { | r, i | > r*i > } > > (1..100).inject(1) do | r, i | > puts "#{i}! = #{r = r*i}" > end inject clearly does what is supposed to. I'm not referring to it. I'm referring to the following loops that are similar to plain while/until/loop loops: enum.each {|o| ...} -> enum or break value enum.each_with_index {|o,i| ...} -> enum or break value for o in enum; ... end -> enum or break value int.times {|i| ...} -> int or break value int.downto(stop) {|i| ...} -> int or break value int.upto(stop) {|i| ...} -> int or break value int.step(stop,step) {|i| ...} -> int or break value Compare this to the traditional loops: loop { ... } -> nil or break value while ...; ... end -> nil or break value until ...; ... end -> nil or break value begin ... end while ... -> nil or break value begin ... end until ... -> nil or break value I guess many may not know that break can take a value as an argument and make the broken loop return that. I find this behavior quite useful except for those loops that return something other than nil in the no-break case (the object loops above). What about the Integer loops above? Anybody using what those return now - the original int? Maybe I could propose just those loops. __________________________________ Yahoo! Mail Mobile Take Yahoo! Mail with you! Check email on your mobile phone. http://mobile.yahoo.com/learn/mail