Turning string into predicate in Ruby
ActiveSupport::StringInquirer provides a nice way to test strings for equality via predicate methods:
env = ActiveSupport::StringInquirer.new('production')
env.production?
#> true
env.development?
#> false
Rails extends String class with String#inquiry to convert any string into ActiveSupport::StringInquirer
object:
env = 'production'.inquiry
env.production?
#> true
env.class
#> ActiveSupport::StringInquirer
Another nice thing about ActiveSupport::StringInquirer
is that, it can be used in places where regular String
is used for comparisons with other String
values:
env = 'production'.inquiry
env == 'production'
#> true
env == 'development'
#> false