The code executes different method according to the environment, in Rails. In this way, you can show message only in development environment.
Judge Environment
Rails.env.development?
, Rails.env.production?
returns true
/false
.
1 2 3 |
if Rails.env.development? // what you want to do end |
What is Rails.env
?
Rails.env
is not String
, it is ActiveSupport::StringInquirer
extending String
. StringInquirer
is used as follows.
1 2 3 |
vehicle = ActiveSupport::StringInquirer.new('car') vehicle.car? # => true vehicle.bike? # => false |
Then, how StringInquirer
is implemented?
Here is ActiveSupport::StringInquirer
in master branch on .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# frozen_string_literal: true module ActiveSupport class StringInquirer < String private def respond_to_missing?(method_name, include_private = false) (method_name[-1] == "?") || super end def method_missing(method_name, *arguments) if method_name[-1] == "?" self == method_name[0..-2] else super end end end end |
It can use all methods String
has.