From: Jonas Bengtsson Date: 2002-07-09T21:19:30+09:00 Subject: Re: for ... else ... end Hello David, What about: while very_time_consuming_operation do_something else do_something_else end or: while a and b and c or d and e and f and g #and so on, i.e. a complex #condition do_something else do_something_else end I know it's possible to rewrite those snippets without using an else-construct. But I think they are easier to understand. -- Best regards, Jonas The brain is a wonderful organ. It starts working the moment you get up in the morning, and does not stop until you get into the office. - Robert Frost Tuesday, July 09, 2002, 12:28:32 PM, you wrote: > Hi -- > On Tue, 9 Jul 2002, Sean Chittenden wrote: >> Hmm... I work around not having this construct constantly when doing >> database work. After talking with Holden about this on IRC, I think >> the following examples are helpful. >> >> >> dbh.select_all(sql) do |row| >> # do something with row >> else >> puts "No rows returned!" >> end > To me this introduces a seriously criss-crossed quality to the logic > flow. You'd end up with things like: > arr = [1,2,3,4] > f = arr.find_all {|n| n > 4 else puts "Not found"} >> for elt in arr >> puts elt >> else >> puts "No rows processed" >> end >> >> >> >> Which opens the door for: >> >> for elt in arr >> puts elt >> else >> puts "no rows" >> ensure >> puts "done looping" >> end >> >> >> Hmm... I think else is the correct word because it is short hand for >> typing out: >> >> if [].length > 0 >> for elt in [] >> puts elt >> end >> else >> puts "now rows" >> end >> puts "done looping" >> >> The example immediately above strikes me as a way of working around >> the language, as opposed to the language doing what you want it to. >> ::shrug:: My $0.02. -sc > puts if arr.empty? then "no rows" else arr end > puts "done looping" > (Couldn't resist :-) > How about: > if arr.empty? > puts "no rows" > else > for elt in arr > # do stuff > end > end > But in any case, I agree with Det who pointed out that this > (determining whether a loop is executed/entered at all) is different > from the 'break'-trapping scenario. > David