# How to deal with boolean parameters in Ruby

Source: https://www.ironin.it/blog/how-to-deal-with-boolean-parameters-in-ruby.html

Published 21 April 2017

[back-end development](https://www.ironin.it/blog/tags/back-end-development) [best practices](https://www.ironin.it/blog/tags/best-practices) [ruby](https://www.ironin.it/blog/tags/ruby)

![How to deal with boolean parameters in Ruby](https://www.ironin.it/api/media/file/ironin-seed-YTYwODdhYmMtN2E3Zi00ZjEyLTg1ZTctMzRmYWQ5OTA5ZjY3_blog-img6.jpg)

Today, one of our experts shares their thoughts on how to pass boolean arguments and assign default values to them. As employees of a top [Ruby on Rails development company](https://www.ironin.it/services/ruby-on-rails-development), our developers deepen and share their expertise daily within the team. We also like to share our knowledge online, which has resulted in this article. Follow our blog for more.

## Problem

Imagine you are working with a calculator class that sums processed payment amounts. That's the only thing it does by default.

But what if you also want to include processing payments - and not always by default?

## Solution

You can pass an argument to perform processing in several ways — for example, as a function parameter, or a hash argument.

If you're dealing with boolean arguments, you can use one of these strategies for assigning a default value:

a) Keyword arguments - they're available in the Ruby 2.0+ version. Here's an example of a keyword argument:

```ruby
class Calculator
  def initialize(user:, include_processing_payments: true)
    @user = user
    @include_processing_payments = include_processing_payments
  end
end
```

b) A hash - you typically use a hash to pass multiple arguments or additional values. Here's what it looks like in our example:

```ruby
class Calculator
  def initialize(options)
    @user = options[:user]
    # the second argument of fetch is going to be the default value
    @include_processing_payments = options.fetch(:include_processing_payments, true)
  end
end
```

Now, both solutions might look pretty neat at first glance. But can you be 100% sure they will handle every possible default value?

The strategies will work for cases where you pass true or false; there's no doubt about it.

### What about nil?

The first approach I showed you assign the default value to the parameter only when it's not passed. So even if you pass `nil`, you're still not going to get a default value assigned.

The second approach looks promising, but it's not going to work either when you try to pass `nil` in your hash explicitly: `{ include_processing_payments: nil }`.

Is there a way to fix that problem and ensure you get a boolean value assigned correctly?

Consider following two strategies.

#### Double negation

```ruby
class Calculator
  def initialize(user:, include_processing_payments: true)
    @user = user
    @include_processing_payments = !!include_processing_payments
  end
end
```

In this solution, you'll be negating the parameter value twice. When you pass a value, you can be sure that it's turned into a true or false.

For example, if you pass a `nil` value, you're going to get a false. And that's what you'll be looking for most of the time.

#### Compact method

```ruby
class Calculator
  def initialize(options)
    @user = options[:user]
    @include_processing_payments = options.compact.fetch(:include_processing_payments, true)
  end
end
```

For hash parameters, you can try using a `compact` method. It removes all the key-value pairs where the value is `nil`. This way, the fetch method won't be able to find the required key and so will assign true as the default value.

However, this approach doesn't work for arguments that are not supplied with `nil` or boolean values. To support this kind of case, you'll still need double negation: `!!options.fetch(:include_processing_payments, true)`.

Note: You might spot that type of code snippet in your code:

```ruby
class Calculator
  def initialize(user:, include_processing_payments: true)
    @user = user
    @include_processing_payments = include_processing_payments || true
  end
end
```

It might look correct initially, but you should avoid that kind of implementation. Sure, it solves your problem with the `nil` value, but it also generates a new issue. The `include_processing_payments` instance variable will always be evaluated as true, even if you pass `false` instead of `nil` as an argument.

## Key takeaways

Boolean arguments can be tricky.

That's why you should always take extra care when passing them in your code. Just choose the proper implementation method to avoid problems when assigning default values from boolean arguments .If you are encountering issues, need help in resolving technical issues, or need additional Ruby on Rails experts to team up with your team - [contact us](https://www.ironin.it/contact-us.html).

Share:

Author: iRonin IT Team

Need software like this built?

[Software development →](https://www.ironin.it/ai-software-engineering)

Our code is merged into 10 open-source AI-agent projects, two of them with tens of thousands of GitHub stars.

[Talk to us →](https://www.ironin.it/contact-us.html?offer=ai-software-engineering)

## Read similar articles

- [Intro to Arel - the database agnostic SQL](https://www.ironin.it/blog/intro-to-arel-the-database-agnostic-sql.html)
- [Smart Dispatch, Happy Customers: How AI Dispatch Software Benefits Everyone](https://www.ironin.it/blog/ai-dispatch-software.html)
- [Artificial Intelligence & Machine Learning - practical use in applications](https://www.ironin.it/blog/artificial-intelligence-machine-learning-practical-use-in-apps.html)
- [Selection of tools for development and support of custom web apps handling 200K requests a month.](https://www.ironin.it/blog/tools-for-development-of-custom-web-app-handling-thousands-requests-a-month.html)

## Read next

- [Is this the cryptopocalypse? Who needs to act on post-quantum cryptography now](https://www.ironin.it/blog/post-quantum-cryptography-who-must-act-now.html)
- [Which AI model is best for your own task? Our own test and a public leaderboard disagreed](https://www.ironin.it/blog/own-ai-model-evals.html)
- [What uses up a Claude plan's usage limit? What each kind of token costs against it](https://www.ironin.it/blog/claude-usage-limit-token-costs.html)
