Ylan Segal

StringInquirer: Nicer Syntax for Testing Equality

There is a well-known idiom in Rails, to tell wheather one is runnign in a specifc environment or not:

1
2
Rails.env.production? # => false
Rails.env.development? # => true

At first, it looks like Rails.env is a special kind of object that has methods defined on it to check for the environment properties. However, upon closer inspection, it looks like it is really just a String, but not quite:

1
2
3
4
>> Rails.env
=> "development"
>> Rails.env.class
=> ActiveSupport::StringInquirer

As the documentation says, StringInquirer is just a pretty way to test for equality. It can be used outside of Rails, say for example to be a bit lenient when reading environment variables:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
require 'active_support/core_ext/string'

class Logger
  def log(message)
    message if verbose?
  end

  private

  def verbose?
    env_verbose.true? || env_verbose.yes?
  end

  def env_verbose
    @env_verbose ||= (ENV['VERBOSE'] ||= '').downcase.inquiry
  end
end

Logger.new.log("Hello, World") # => nil

ENV['VERBOSE'] = 'yes'
Logger.new.log("Hello, World") # => "Hello, World"

ENV['VERBOSE'] = 'YES'
Logger.new.log("Hello, World") # => "Hello, World"

ENV['VERBOSE'] = 'True'
Logger.new.log("Hello, World") # => "Hello, World"