Today I Learned

hashrocket A Hashrocket project

Test for String Equality

In Rails, there is an nice and easy interface that can be used to check a string for equality.

To do this, you can use the ActiveSupport::StringInquirer object. Simply initialize it with a string value, like so:

string = ActiveSupport::StringInquirer.new("vanilla")

and now we can just call string#vanilla? to check for equality

string.vanilla?
=> true

string.chocolate?
=> false

Fun fact:
This is how you are able to do Rails.env.production?

H/T: Matt Polito

See More #rails TILs
Looking for help? Hashrocket has been an industry leader in Ruby on Rails since 2008. Rails is a core skill for each developer at Hashrocket, and we'd love to take a look at your project. Contact us and find out how we can help you.