41

I know I could easily write a function and put it in the application controller, but I'd rather not if there is something else that does this already. Basically I want to have something like:

>> boolean_variable?
=> true
>> boolean_variable?.yesno
=> yes
>> boolean_variable?.yesno.capitalize
=> Yes

is there something like this already in the Rails framework?

1

6 Answers 6

57

There isn't something in Rails.

A better way than adding to the true/false classes to achieve something similar would be to make a method in ApplicationHelper:

def human_boolean(boolean)
    boolean ? 'Yes' : 'No'
end

Then, in your view

<%= human_boolean(boolean_youre_checking) %>

Adding methods to built-in classes is generally frowned upon. Plus, this approach fits in closely to Rails' helpers like raw().

Also, one offs aren't a great idea as they aren't easily maintained (or tested).

0
40

No such built-in helper exists, but it's trivially easy to implement:

class TrueClass
  def yesno
    "Yes"
  end
end

class FalseClass
  def yesno
    "No"
  end
end
7
  • 1
    Hey, that's slicker then what I would have come up with. As far as a rails app is concerned, where would the best place to put this code be? in a file under lib or something?
    – aarona
    Commented Sep 8, 2010 at 5:05
  • I would create a custom initializer in the config/initializer path called "boolean_path.rb". That way, these new methods would be available through the app. Cheers!
    – Eytan
    Commented Sep 24, 2012 at 19:03
  • 1
    @Eytan I'm new to ruby and rails and I'm trying to stick with convention but I don't know how to create an intializer, can you help me with it? Basically what would I put in that boolean_path.rb and how can I use it? What I would like to do is when I create my model, I would like to use t.boolean_yesno instead of regular boolean and have that output Yes or No instead of True or False. If you prefer I open another question for this let me know.
    – GiH
    Commented Feb 22, 2013 at 6:18
  • 2
    Lets answer your question here because it fits in nicely with this question. To create an initializer, all you need to do is add a .rb file in the config/initializers folder and literally copy and paste the above code into it. Now, whenever you are working with a boolean variable, you can call: my_boolean_variable.yesno and it will return "Yes" or "No".
    – Eytan
    Commented Feb 25, 2013 at 3:14
  • 1
    @CorySimmons It's not super likely unless you choose a method name that conflicts with something else. yesno is probably safe-ish.
    – Jarin Udom
    Commented May 14, 2014 at 15:18
23

There is a gem for that now: humanize_boolean

Then you just do:

true.humanize # => "Yes" 
false.humanize # => "No"

It also supports internationalization so you can easily change the returned string by including your translation for en.boolean.yes and en.boolean.no (or any locale you like)

1
  • 12
    A gem seems a bit egregious for this, no?
    – richessler
    Commented Dec 18, 2018 at 21:05
19

The humanize_boolean gem extends the TrueClass, FalseClass and NilClass which is an indirect extension I would prefer to avoid.

I've found this helper with a top level translation is isolated, friendly to change and you can give it anything truthy or falsey:

# app/helpers/application_helper.rb
class ApplicationHelper
  def humanize_boolean(boolean)
    I18n.t((!!boolean).to_s)
  end
end

# config/locales/en.yml
en:
  :true: 'Yes'
  :false: 'No'

Doing !!(boolean).to_s ensures the variables passed to the argument is either a "true" or "false" value when building the translation string.

In use:

# app/views/chalets/show.html.erb
<%= humanize_boolean(chalet.rentable?) %>
17

Alternatively, you could also do one offs in your views such as:

<%= item.bool_field? ? 'yes' : 'no' %>
3
  • 2
    This isn't very DRY, compared to the other solution and will be difficult to localize. Commented Oct 25, 2010 at 15:48
  • 5
    No, Jeff, you're absolutely right. I was speaking to one offs, and not to many offs, and in the case of the latter, a helper would of course be the wiser (and DRY) option, as you point out. Commented Oct 25, 2010 at 16:09
  • Perfect! I have a one form app, so I don't care about localization/DRY (yet). Thanks for that. Commented May 29, 2013 at 18:16
4

I love the generalised solutions in the previous answers, but if you want to selectively use a humanised version of your boolean in your views, you could do the following:

I put this into helpers/application_helper.rb:

module ApplicationHelper

  def humanize_boolean(value)
    case value
    when true
      "Yes"
    when false
      "No"
    when nil
      "Undefined"
    else
      "Invalid"
    end
  end

end

Then from views you can call it as in this example:

Published: <%= humanize_boolean(post.published) %>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.