How to Convert Strings to Booleans in Ruby

Daniel Pericich
Geek Culture
Published in
3 min readMay 27, 2021
Photo by Fleur on Unsplash

While working on a recent project, I came upon a guard clause acting on an API response. My app returned a specific message to the user based on whether or not the API gave back a value of true or false. Whether this API was unstable or the previous developer was just cautious, I found a “to_b” appended to the end of the attribute we were checking. While I had worked with “to_b” before, I had never dove into it’s inner workings, and was curious to learn more.

One of the great challenges of interacting with 3rd party APIs is getting both your logic and the API’s responses to match. API responses pull from a number of different types of databases, and these databases store different possible values for what true or false may be. When an API sends a response object with a boolean value, we have to ask is true a string “true”, a 1 or even just a boolean of true? Because this is such a common problem, Ruby offers the “to_b” string method.

What is the to_b method?

The “to_b” method is a string specific method that Ruby uses to evaluate objects to boolean values. This method is incredibly value in that it can be added to any string to determine if the value is true or false. This is especially valuable with JSON responses as all values in a JSON object are strings. In order to use the “to_b” method you will need to add the facets gem found here which extends the base Ruby objects’ methods.

To understand the response of this method, we must know that there are three possible responses from “string.to_b”: true, false and nil.

Let’s first look at the strings that produce nil. If you call “to_b” on “nil” or “null”, you will be returned nil. Ruby does not use “null” so why should it produce a response of “nil”? This is a good point, and while Ruby does not use “null”, JavaScript does, so it will return a lack of an assigned value as “null” which can then be converted to “nil” for use in Ruby.

The next possible outcome of “to_b” is true. True can be represented as many things in programming, but there are only certain strings that “to_b” will convert to a boolean value of true. They are the following:

Figure 1. All strings that will evaluate to true

The final possible return value for “to_b” covers every other string. Calling “to_b” on any other string, besides those listed above, will return a value of “false”. If we run any of the following strings with “to_b” we will always get false:

Figure 2. Some of the many strings that evaluate to false

Conclusion

Now that you know more about the “to_b” string method, hopefully you can better integrate logic coming from API responses. Let me know in the comments how you are going to use “to_b” to refactor your existing code, or to improve the resiliency of future projects!

Notes

https://www.rubydoc.info/github/rubyworks/facets/String:to_b

--

--

Daniel Pericich
Geek Culture

Former Big Beer Engineer turned Full Stack Software Engineer