# Begin else pattern question

**URL:** <https://www.ruby-forum.com/t/begin-else-pattern-question/154163>\
**Category:** Ruby\
**Created:** [November 26, 2008, 12:24am UTC](https://www.ruby-forum.com/t/begin-else-pattern-question/154163 "2008-11-26T00:24:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mikel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/2bfe46/32.png) [@Mikel\_L](https://www.ruby-forum.com/u/Mikel_L)\
**Post date:** [November 26, 2008, 12:24am UTC](https://www.ruby-forum.com/t/begin-else-pattern-question/154163/1 "2008-11-26T00:24:46Z")

</div>

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?

Mikel

---

<div class="post-metadata">

**Author:** ![Mikel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/2bfe46/32.png) [@Mikel\_L](https://www.ruby-forum.com/u/Mikel_L)\
**Post date:** [November 26, 2008, 1:15am UTC](https://www.ruby-forum.com/t/begin-else-pattern-question/154163/2 "2008-11-26T01:15:39Z")

</div>

On Nov 25, 6:19 pm, Mikel L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Any other simpler way to do this pattern?

Hi Mikel,

One simplification would be to remove the “begin” and the  
corresponding “end” since they’re not needed in this situation.

An extreme approach would be to put the basic logic structure into a  
method. But then you’d have to pass in three Procs – one for the  
boolean expression, and one each for a\_action and b\_action.

If you just want to avoid repeating the b\_action, you could raise an  
exception in your else to force yourself into the rescue clause and do  
the b\_action there.

Hope there’s something in there that you find helpful!

Eric

====

Interested in hands-on, on-site Ruby or Ruby on Rails  
training? See [http://LearnRuby.com](http://LearnRuby.com) for information about a  
well-reviewed courses.

---

<div class="post-metadata">

**Author:** ![Mikel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/2bfe46/32.png) [@Mikel\_L](https://www.ruby-forum.com/u/Mikel_L)\
**Post date:** [November 26, 2008, 1:16am UTC](https://www.ruby-forum.com/t/begin-else-pattern-question/154163/3 "2008-11-26T01:16:14Z")

</div>

On Tue, Nov 25, 2008 at 3:19 PM, Mikel L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> end
> 
> Any other simpler way to do this pattern?
> 
> Mikel

A device that turns exceptions into a return value might assist.  
Perhaps something like this:

def succeeded  
begin  
yield  
true  
rescue  
false  
end  
end

def a\_action(succeed)  
raise “failed” if !succeed  
print “a\_action\n”  
end

def b\_action  
print “b\_action\n”  
end

def action(value, succeed)  
b\_action if !(value == “blah” && succeeded { a\_action(succeed) })  
end

action “blah”, true  
action “blah”, false  
action “not blah”, true  
action “not blah”, false

---

<div class="post-metadata">

**Author:** ![Mikel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/2bfe46/32.png) [@Mikel\_L](https://www.ruby-forum.com/u/Mikel_L)\
**Post date:** [November 26, 2008, 2:07am UTC](https://www.ruby-forum.com/t/begin-else-pattern-question/154163/4 "2008-11-26T02:07:43Z")

</div>

On Wed, Nov 26, 2008 at 11:10 AM, Eric I. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> On Nov 25, 6:19 pm, Mikel L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Any other simpler way to do this pattern?  
> > One simplification would be to remove the “begin” and the  
> > corresponding “end” since they’re not needed in this situation.

Actually… that cleans it up enough and makes it nice and readable.  
def initialize(raw\_field)  
if a  
a\_action  
else  
b\_action  
end  
rescue  
b\_action  
end

Why aren’t the begin and end blocks needed? Is it because it is already  
encapsulated within a method any way and if it hits rescue, the rescue  
block  
is terminated by the method end in any case?

What I am trying to ask is “where did you learn when the begin and end  
are  
needed and when not?” 🙂

---

<div class="post-metadata">

**Author:** ![Mikel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/2bfe46/32.png) [@Mikel\_L](https://www.ruby-forum.com/u/Mikel_L)\
**Post date:** [November 26, 2008, 4:10am UTC](https://www.ruby-forum.com/t/begin-else-pattern-question/154163/5 "2008-11-26T04:10:31Z")

</div>

On Nov 25, 8:02 pm, Mikel L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Why aren’t the begin and end blocks needed? Is it because it is already  
> encapsulated within a method any way and if it hits rescue, the rescue block  
> is terminated by the method end in any case?
> 
> What I am trying to ask is “where did you learn when the begin and end are  
> needed and when not?” 🙂

Hi Mikel,

It’s in the reference section of the “pickaxe” book (in my edition  
it’s p. 346).

Basically there are three cases when you can use a rescue:

1. Directly within a begin/end block.
2. At the top-level of a method.
3. Attached to a single statement.

Hope that helps,

Eric

====

Interested in hands-on, on-site Ruby or Ruby on Rails  
training? See [http://LearnRuby.com](http://LearnRuby.com) for information about a  
well-reviewed courses.

---

<div class="post-metadata">

**Author:** ![Mikel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/2bfe46/32.png) [@Mikel\_L](https://www.ruby-forum.com/u/Mikel_L)\
**Post date:** [November 26, 2008, 4:25am UTC](https://www.ruby-forum.com/t/begin-else-pattern-question/154163/6 "2008-11-26T04:25:43Z")

</div>

From: Mikel L. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# 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.

and what if b\_action throws, rescue it also w b\_action ??

maybe, something like,

def initialize(value)  
if value == ‘blah’  
begin  
a\_action  
rescue  
b\_action  
end  
else  
b\_action  
end  
end

or, if you like a flag and ensure

def initialize(value)  
a\_action if flag = (value == ‘blah’)  
rescue  
flag = false  
ensure  
b\_action unless flag  
end

---

<div class="post-metadata">

**Author:** ![Mikel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/2bfe46/32.png) [@Mikel\_L](https://www.ruby-forum.com/u/Mikel_L)\
**Post date:** [November 27, 2008, 7:23pm UTC](https://www.ruby-forum.com/t/begin-else-pattern-question/154163/7 "2008-11-27T19:23:42Z")

</div>

2008/11/26 Mikel L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> end
> 
> 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

---

<div class="post-metadata">

**Author:** ![Mikel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/2bfe46/32.png) [@Mikel\_L](https://www.ruby-forum.com/u/Mikel_L)\
**Post date:** [November 26, 2008, 1:09pm UTC](https://www.ruby-forum.com/t/begin-else-pattern-question/154163/8 "2008-11-26T13:09:12Z")

</div>

Here’s another option for removing the duplication of b\_action:

def initialize(value)  
begin  
if value == ‘blah’  
a\_action  
return  
end  
rescue  
end  
b\_action  
end

This is not identical to your original code, which would call b\_action  
twice if an exception occurred during the first invocation of b\_action.

If a\_action is short you could fold the first four lines to

```
return a_action if value == 'blah'

```

Note that a bare ‘rescue’ is the same as ‘rescue StandardError’. Certain  
exceptions, typically due to lack of system resources, are not caught.  
Often that is what you want, but to catch absolutely everything do

```
rescue Exception

```

Finally, there is the expression version of rescue, which I believe  
catches StandardError:

def initialize(value)  
(return a\_action if value == ‘blah’) rescue nil  
b\_action  
end

Regards,

Brian.
