Sets the named constant to the given object, returning that object. Creates a new constant if no constant with the given name previously existed.
Math.const_set("HIGH_SCHOOL_PI", 22.0/7.0) #=> 3.14285714285714 Math::HIGH_SCHOOL_PI - Math::PI #=> 0.00126448926734968
If sym
or str
is not a valid constant name a NameError
will be raised with a warning “wrong constant name”.
Object.const_set('foobar', 42) #=> NameError: wrong constant name foobar
Says whether mod or its ancestors have a constant with the given name:
Float.const_defined?(:EPSILON) #=> true, found in Float itself Float.const_defined?("String") #=> true, found in Object (ancestor) BasicObject.const_defined?(:Hash) #=> false
If mod is a Module
, additionally Object
and its ancestors are checked:
Math.const_defined?(:String) #=> true, found in Object
In each of the checked classes or modules, if the constant is not present but there is an autoload for it, true
is returned directly without autoloading:
module Admin autoload :User, 'admin/user' end Admin.const_defined?(:User) #=> true
If the constant is not found the callback const_missing
is not called and the method returns false
.
If inherit
is false, the lookup only checks the constants in the receiver:
IO.const_defined?(:SYNC) #=> true, found in File::Constants (ancestor) IO.const_defined?(:SYNC, false) #=> false, not found in IO itself
In this case, the same logic for autoloading applies.
If the argument is not a valid constant name a NameError
is raised with the message “wrong constant name name”:
Hash.const_defined? 'foobar' #=> NameError: wrong constant name foobar
Removes the definition of the given constant, returning that constant’s previous value. If that constant referred to a module, this will not change that module’s name and can lead to confusion.
Invoked when a reference is made to an undefined constant in mod. It is passed a symbol for the undefined constant, and returns a value to be used for that constant. The following code is an example of the same:
def Foo.const_missing(name) name # return the constant name as Symbol end Foo::UNDEFINED_CONST #=> :UNDEFINED_CONST: symbol returned
In the next example when a reference is made to an undefined constant, it attempts to load a file whose name is the lowercase version of the constant (thus class Fred
is assumed to be in file fred.rb
). If found, it returns the loaded class. It therefore implements an autoload feature similar to Kernel#autoload
and Module#autoload
.
def Object.const_missing(name) @looked_for ||= {} str_name = name.to_s raise "Class not found: #{name}" if @looked_for[str_name] @looked_for[str_name] = 1 file = str_name.downcase require file klass = const_get(name) return klass if klass raise "Class not found: #{name}" end
Returns an array of the names of class variables in mod. This includes the names of class variables in any included modules, unless the inherit parameter is set to false
.
class One @@var1 = 1 end class Two < One @@var2 = 2 end One.class_variables #=> [:@@var1] Two.class_variables #=> [:@@var2, :@@var1] Two.class_variables(false) #=> [:@@var2]
Makes a list of existing constants public.
Makes a list of existing constants private.
Makes a list of existing constants deprecated. Attempt to refer to them will produce a warning.
module HTTP NotFound = Exception.new NOT_FOUND = NotFound # previous version of the library used this name deprecate_constant :NOT_FOUND end HTTP::NOT_FOUND # warning: constant HTTP::NOT_FOUND is deprecated
Returns true
if mod is a singleton class or false
if it is an ordinary class or module.
class C end C.singleton_class? #=> false C.singleton_class.singleton_class? #=> true
Execute the provided block, but preserve the precision limit
BigDecimal.limit(100) puts BigDecimal.limit BigDecimal.save_limit do BigDecimal.limit(200) puts BigDecimal.limit end puts BigDecimal.limit
Returns the number of decimal significant digits in self
.
BigDecimal("0").n_significant_digits # => 0 BigDecimal("1").n_significant_digits # => 1 BigDecimal("1.1").n_significant_digits # => 2 BigDecimal("3.1415").n_significant_digits # => 5 BigDecimal("-1e20").n_significant_digits # => 1 BigDecimal("1e-20").n_significant_digits # => 1 BigDecimal("Infinity").n_significant_digits # => 0 BigDecimal("-Infinity").n_significant_digits # => 0 BigDecimal("NaN").n_significant_digits # => 0
Converts a BigDecimal
to a String
of the form “nnnnnn.mmm”. This method is deprecated; use BigDecimal#to_s
(“F”) instead.
require 'bigdecimal/util' d = BigDecimal("3.14") d.to_digits # => "3.14"
Methods BigDecimal#as_json
and BigDecimal.json_create
may be used to serialize and deserialize a BigDecimal object; see Marshal
.
Method BigDecimal#as_json
serializes self
, returning a 2-element hash representing self
:
require 'json/add/bigdecimal' x = BigDecimal(2).as_json # => {"json_class"=>"BigDecimal", "b"=>"27:0.2e1"} y = BigDecimal(2.0, 4).as_json # => {"json_class"=>"BigDecimal", "b"=>"36:0.2e1"} z = BigDecimal(Complex(2, 0)).as_json # => {"json_class"=>"BigDecimal", "b"=>"27:0.2e1"}
Method JSON.create
deserializes such a hash, returning a BigDecimal object:
BigDecimal.json_create(x) # => 0.2e1 BigDecimal.json_create(y) # => 0.2e1 BigDecimal.json_create(z) # => 0.2e1
Returns a JSON
string representing self
:
require 'json/add/bigdecimal' puts BigDecimal(2).to_json puts BigDecimal(2.0, 4).to_json puts BigDecimal(Complex(2, 0)).to_json
Output:
{"json_class":"BigDecimal","b":"27:0.2e1"} {"json_class":"BigDecimal","b":"36:0.2e1"} {"json_class":"BigDecimal","b":"27:0.2e1"}
Methods Rational#as_json
and Rational.json_create
may be used to serialize and deserialize a Rational object; see Marshal
.
Method Rational#as_json
serializes self
, returning a 2-element hash representing self
:
require 'json/add/rational' x = Rational(2, 3).as_json # => {"json_class"=>"Rational", "n"=>2, "d"=>3}
Method JSON.create
deserializes such a hash, returning a Rational object:
Rational.json_create(x) # => (2/3)
Returns a JSON
string representing self
:
require 'json/add/rational' puts Rational(2, 3).to_json
Output:
{"json_class":"Rational","n":2,"d":3}
Return the accept character set for all new CGI
instances.
Equivalent to >>
with argument n
.
Equivalent to <<
with argument n
.
Equivalent to >>
with argument n * 12
.
Equivalent to <<
with argument n * 12
.
Returns a hash of the name/value pairs, to use in pattern matching. Possible keys are: :year
, :month
, :day
, :wday
, :yday
.
Possible usages:
d = Date.new(2022, 10, 5) if d in wday: 3, day: ..7 # uses deconstruct_keys underneath puts "first Wednesday of the month" end #=> prints "first Wednesday of the month" case d in year: ...2022 puts "too old" in month: ..9 puts "quarter 1-3" in wday: 1..5, month: puts "working day in month #{month}" end #=> prints "working day in month 10"
Note that deconstruction by pattern can also be combined with class check:
if d in Date(wday: 3, day: ..7) puts "first Wednesday of the month" end