Skip to content

Instantly share code, notes, and snippets.

def env
@_env ||= begin
require 'active_support/string_inquirer'
ActiveSupport::StringInquirer.new(RAILS_ENV)
end
end
class Momoro < ActiveRecord::Base
def food
ActiveSupport::StringInquirer.new("#{read_attribute(:food)}")
end
end
# sweet!
>> m.food = "hi"
=> "hi"
>> m.food.hi?
=> true
>> m.food.apple?
=> false
>> m.food = "apple"
=> "apple"
>> m.food.apple?
class Momoro < ActiveRecord::Base
def food
ActiveSupport::StringInquirer.new(read_attribute(:food))
end
end
>> m = Momoro.new
=> #<Momoro id: nil, name: nil, food: nil, created_at: nil, updated_at: nil>
>> m.food = "basil"
=> "basil"
>> m.food.basil?
method name = basil?
method_name.to_s[-1,1] = ?
self = basil
method_name.to_s[0..-2] = basil
=> true
=> #<Momoro id: 5, name: nil, food: nil, created_at: "2008-12-09 07:38:34", updated_at: "2008-12-09 07:38:34">
>> m.food
=> "basil"
>> m.read_attribute(:food)
=> nil
module ActiveSupport
# Wrapping a string in this class gives you a prettier way to test
# for equality. The value returned by <tt>Rails.env</tt> is wrapped
# in a StringInquirer object so instead of calling this:
#
# Rails.env == "production"
#
# you can call this:
#
# Rails.env.production?
class Momoro < ActiveRecord::Base
def food
ActiveSupport::StringInquirer.new("basil")
end
end
# It doesn't work this way!
>> m = Momoro.new
=> #<Momoro id: nil, name: nil, food: nil, created_at: nil, updated_at: nil>
>> m.food = "basil"
>> m.food.basil?
=> true
>> m.food = "pillows"
>> m.food.pillows?
>> !!active
=> true
>> active = false
=> false
>> !!active
=> false
>> active = nil