# How to avoid repeating the same code depending if a method param is nil or not?

**URL:** <https://www.ruby-forum.com/t/how-to-avoid-repeating-the-same-code-depending-if-a-method-param-is-nil-or-not/234444>\
**Category:** Ruby\
**Created:** [March 5, 2015, 12:50am UTC](https://www.ruby-forum.com/t/how-to-avoid-repeating-the-same-code-depending-if-a-method-param-is-nil-or-not/234444 "2015-03-05T00:50:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [March 5, 2015, 12:50am UTC](https://www.ruby-forum.com/t/how-to-avoid-repeating-the-same-code-depending-if-a-method-param-is-nil-or-not/234444/1 "2015-03-05T00:50:24Z")

</div>

Hi everyone, I keep coming across this scenario where I have a method  
that takes multiple parameters, most are optional. Within the method I  
have either a call to another method, or I do something with the params.  
It bothers me having to write the same call multiple times depending on  
if a param is nil or not. Say the method I am calling must not take nil  
param because it will assign a nil value (which is not desired).

What suggestions would you guys have for this?

These are my examples:

> **[example:get\_balance(user\_id, user\_state = nil)	if user\_state.blank? Balan -...](https://pastebin.com/56CSmztC)**
>
> Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.

---

<div class="post-metadata">

**Author:** ![streetz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee7513/32.png) [@streetz](https://www.ruby-forum.com/u/streetz)\
**Post date:** [March 5, 2015, 1:17am UTC](https://www.ruby-forum.com/t/how-to-avoid-repeating-the-same-code-depending-if-a-method-param-is-nil-or-not/234444/2 "2015-03-05T01:17:18Z")

</div>

Got a solution. Before using the hash, I can call reject on it and  
remove the nil items. As long as what ever I am calling does not want a  
nil I can use do this.

---

<div class="post-metadata">

**Author:** ![streetz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee7513/32.png) [@streetz](https://www.ruby-forum.com/u/streetz)\
**Post date:** [March 5, 2015, 8:56pm UTC](https://www.ruby-forum.com/t/how-to-avoid-repeating-the-same-code-depending-if-a-method-param-is-nil-or-not/234444/3 "2015-03-05T20:56:46Z")

</div>

Why is user state a parameter in multiple methods, and not an attribute  
of the user?

---

<div class="post-metadata">

**Author:** ![streetz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee7513/32.png) [@streetz](https://www.ruby-forum.com/u/streetz)\
**Post date:** [March 5, 2015, 9:09pm UTC](https://www.ruby-forum.com/t/how-to-avoid-repeating-the-same-code-depending-if-a-method-param-is-nil-or-not/234444/4 "2015-03-05T21:09:18Z")

</div>

These are just examples in attempt to try to explain my problem.

---

<div class="post-metadata">

**Author:** ![streetz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee7513/32.png) [@streetz](https://www.ruby-forum.com/u/streetz)\
**Post date:** [March 23, 2015, 2:10am UTC](https://www.ruby-forum.com/t/how-to-avoid-repeating-the-same-code-depending-if-a-method-param-is-nil-or-not/234444/5 "2015-03-23T02:10:22Z")

</div>

get\_balance(user\_id, user\_state = nil)

What is the class or module which receives the get\_balance() message, or  
what is the class of the object which receives the get\_balance()  
message?

Say that a User has an Account that has a Balance, or some such.

user = User.new(user\_id)

user.balance

user.balance(account: ‘savings’)

You may be encountering a hint that the design needs to be revisited and  
perhaps revised.

---

<div class="post-metadata">

**Author:** ![streetz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee7513/32.png) [@streetz](https://www.ruby-forum.com/u/streetz)\
**Post date:** [March 21, 2015, 11:18am UTC](https://www.ruby-forum.com/t/how-to-avoid-repeating-the-same-code-depending-if-a-method-param-is-nil-or-not/234444/6 "2015-03-21T11:18:10Z")

</div>

Dala Streetz wrote in post #1169657:

> Got a solution. Before using the hash, I can call reject on it and  
> remove the nil items. As long as what ever I am calling does not want a  
> nil I can use do this.

Why use reject? I’d prefer the opposite way:

get\_balance(user\_id, user\_state = nil)  
params = {:user\_id =\> user\_id}  
params[:user\_state] = user\_state if user\_state  
Balance.get(params)  
end

Cheers

robert
