Rails.env.production?
を見て僕が思い浮かべたコード
module Rails # ... def self.env RailsEnvironment.new end # ... end class RailsEnvironment # ... def production? @environment == :production end # ... end
だけどRail.env #=> 'production'は文字列をStringInquirerでくるんで返してるよと言われて浮かんだのが、
class StringInquirer def method_missing(meth, *args) return true if /\A#{meth}\z/ =~ "#{self}?" super end end
で、実際
class StringInquirer < String def method_missing(method_name, *arguments) if method_name.to_s[-1,1] == "?" self == method_name.to_s[0..-2] else super end end end
お腹いっぱい!