• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Ruby / Using Ruby to Convert String to Boolean

Using Ruby to Convert String to Boolean

October 17, 2022 Leave a Comment

We can use Ruby to convert a string to boolean with the help of the empty? method. We first need to determine if the string is empty or non-empty. Non-empty strings will be converted to true and empty strings will be converted to false.

some_string = "This is a string"
empty_string = ""

some_string = !some_string.empty?
empty_string = !empty_string.empty?

puts some_string
puts empty_string

#Output:
true
false

We can put this code in a method we create called convert_string_to_bool, so that all we have to do is run this method on a string to convert it to a boolean.

def convert_string_to_bool(str)
  return !str.empty?
end

And now let’s run an example with this method to see that it works.

def convert_string_to_bool(str)
  return !str.empty?
end

puts convert_string_to_bool("This is a string")
puts convert_string_to_bool("")

#Output
true
false

If we just want to check if a string is equal to “true”, then we can perform a check with the equality operator ==.

true_string = "true"

print true_string == "true"

#Output:
true

When working with different object types in Ruby, the ability to convert objects to other object types easily is valuable. One such case is if you want to convert a string to a boolean. To convert a string object to a boolean object in Ruby, we can use the Ruby empty? method.

The empty? method will return true if a string is empty, and false if it is not empty. So we just have to set the value of our string to the opposite of what the empty? method returns.

Below is our method again to help convert strings to boolean objects in Ruby.

def convert_string_to_bool(str)
  return !str.empty?
end

non_empty_string = "This is a string"
empty_string = ""

non_empty_string = convert_string_to_bool(non_empty_string)
empty_string = convert_string_to_bool(empty_string)

puts non_empty_string
puts empty_string

#Output
true
false

Checking if String is Equal to true in Ruby

If we just want to check if a string is equal to “true”, then we can perform a check with the equality operator ==. This can be the case if we have user input and want to see if the user input is “true” or “false”.

As we know from above, a string with the value “true” will resolve to true when converted to a boolean value. Therefore, we just need to do a simple check to verify the value of the string object. Below is a simple example showing you how to check if a string is equal to “true” in Ruby.

true_string = "true"

print true_string == "true"

#Output:
true

Hopefully this article has been useful for you to learn how to use Ruby to convert a string to a boolean.

Other Articles You'll Also Like:

  • 1.  Ruby Infinite Loop
  • 2.  How to Square a Number in Ruby
  • 3.  Ruby include Method – Check if Item in Array
  • 4.  Ruby has_key Method – Check if Hash Has Key
  • 5.  How to Print With Newline in Ruby
  • 6.  Ruby Floor – Find Floor of Number with floor Method
  • 7.  Ruby unshift Method – Add Items to Start of Array
  • 8.  Using Ruby to Get the Length of a String
  • 9.  Using Ruby to Print to Console
  • 10.  Get the Sum of an Array in Ruby

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy

x