From: Robert Klemme Date: 2008-11-28T03:18:28+09:00 Subject: Re: Begin else pattern question 2008/11/26 Mikel Lindsaar : > What is the cleanest way of doing: > def initialize(value) > begin > if value == 'blah' > a_action > else > b_action > end > rescue > b_action > end > end > > That is. If a is needed and doing a throws an exception, do b instead. > Otherwise, if a is not needed do b. > > Or is that the cleanest way? > > By cleanest, I mean l don't like calling b_action twice in the method if I > don't have to, but it's a trade off on readability... and I don't _like_ the > way the code looks now :) > > Any other simpler way to do this pattern? def initialize(value) if value == 'blah' a_action rescue b_action else b_action end end Or if you _really_ want to get rid of the second call: def initialize(value) raise "do b!" if value != 'blah' a_action rescue Exception b_action end Cheers robert -- remember.guy do |as, often| as.you_can - without end