DEV Community

Cover image for How to Humanize a Boolean on Ruby
Rixio Barrios
Rixio Barrios

Posted on

How to Humanize a Boolean on Ruby

Having a true or false output is fine when it comes to code no human is ever going to read but, when it comes to the human eye; We want to communicate our answer in the best way UX can handle. There is a simple way to handle this so you can make your method global.

module ApplicationHelper

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

end
Enter fullscreen mode Exit fullscreen mode

Add this handy method to your application_helper.rb and use anywhere on your "views" files.

<%= humanize_boolean(post.published) %>
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
lcobell profile image
Logan CoBell

This was helpful, thank you!