Returns a Digest
subclass by name
in a thread-safe manner even when on-demand loading is involved.
require 'digest' Digest("MD5") # => Digest::MD5 Digest(:SHA256) # => Digest::SHA256 Digest(:Foo) # => LoadError: library not found for class Digest::Foo -- digest/foo
Allows for declaring a Gemfile inline in a ruby script, optionally installing any gems that aren’t already installed on the user’s system.
@note Every gem that is specified in this ‘Gemfile’ will be ‘require`d, as if
the user had manually called `Bundler.require`. To avoid a requested gem being automatically required, add the `:require => false` option to the `gem` dependency declaration.
@param install [Boolean] whether gems that aren’t already installed on the
user's system should be installed. Defaults to `false`.
@param gemfile [Proc] a block that is evaluated as a ‘Gemfile`.
@example Using an inline Gemfile
#!/usr/bin/env ruby require 'bundler/inline' gemfile do source 'https://rubygems.org' gem 'json', require: false gem 'nap', require: 'rest' gem 'cocoapods', '~> 0.34.1' end puts Pod::VERSION # => "0.34.4"
provides a unified clone
operation, for REXML::XPathParser
to use across multiple Object
types
Produces a shallow copy of obj—the instance variables of obj are copied, but not the objects they reference. clone
copies the frozen (unless :freeze
keyword argument is given with a false value) state of obj. See also the discussion under Object#dup
.
class Klass attr_accessor :str end s1 = Klass.new #=> #<Klass:0x401b3a38> s1.str = "Hello" #=> "Hello" s2 = s1.clone #=> #<Klass:0x401b3998 @str="Hello"> s2.str[1,4] = "i" #=> "i" s1.inspect #=> "#<Klass:0x401b3a38 @str=\"Hi\">" s2.inspect #=> "#<Klass:0x401b3998 @str=\"Hi\">"
This method may have class-specific behavior. If so, that behavior will be documented under the #initialize_copy
method of the class.
Returns an array with both a numeric
and a big
represented as Bignum objects.
This is achieved by converting numeric
to a Bignum.
A TypeError
is raised if the numeric
is not a Fixnum or Bignum type.
(0x3FFFFFFFFFFFFFFF+1).coerce(42) #=> [42, 4611686018427387904]
provides a unified clone
operation, for REXML::XPathParser
to use across multiple Object
types
Returns the largest number less than or equal to int
with a precision of ndigits
decimal digits (default: 0).
When the precision is negative, the returned value is an integer with at least ndigits.abs
trailing zeros.
Returns self
when ndigits
is zero or positive.
1.floor #=> 1 1.floor(2) #=> 1 18.floor(-1) #=> 10 (-18).floor(-1) #=> -20
Returns the remainder after dividing int
by numeric
.
x.remainder(y)
means x-y*(x/y).truncate
.
5.remainder(3) #=> 2 -5.remainder(3) #=> -2 5.remainder(-3) #=> 2 -5.remainder(-3) #=> -2 5.remainder(1.5) #=> 0.5
See Numeric#divmod
.
Returns self.
Returns the numerator.
1 2 3+4i <- numerator - + -i -> ---- 2 3 6 <- denominator c = Complex('1/2+2/3i') #=> ((1/2)+(2/3)*i) n = c.numerator #=> (3+4i) d = c.denominator #=> 6 n / d #=> ((1/2)+(2/3)*i) Complex(Rational(n.real, d), Rational(n.imag, d)) #=> ((1/2)+(2/3)*i)
See denominator.
If numeric
is the same type as num
, returns an array [numeric, num]
. Otherwise, returns an array with both numeric
and num
represented as Float
objects.
This coercion mechanism is used by Ruby to handle mixed-type numeric operations: it is intended to find a compatible common type between the two operands of the operator.
1.coerce(2.5) #=> [2.5, 1.0] 1.2.coerce(3) #=> [3.0, 1.2] 1.coerce(2) #=> [2, 1]
Returns the receiver. freeze
cannot be false
.
Returns true
if num
has a zero value.
Returns self
if num
is not zero, nil
otherwise.
This behavior is useful when chaining comparisons:
a = %w( z Bb bB bb BB a aA Aa AA A ) b = a.sort {|a,b| (a.downcase <=> b.downcase).nonzero? || a <=> b } b #=> ["A", "a", "AA", "Aa", "aA", "BB", "Bb", "bB", "bb", "z"]
Returns the largest number less than or equal to num
with a precision of ndigits
decimal digits (default: 0).
Numeric
implements this by converting its value to a Float
and invoking Float#floor
.
Returns the numerator.
Convert self
to locale encoding
Inserts other_str before the character at the given index, modifying str. Negative indices count from the end of the string, and insert after the given character. The intent is insert aString so that it starts at the given index.
"abcd".insert(0, 'X') #=> "Xabcd" "abcd".insert(3, 'X') #=> "abcXd" "abcd".insert(4, 'X') #=> "abcdX" "abcd".insert(-3, 'X') #=> "abXcd" "abcd".insert(-1, 'X') #=> "abcdX"
returns the indexth byte as an integer.
Returns a new string with the characters from str in reverse order.
"stressed".reverse #=> "desserts"
Reverses str in place.
Returns the Symbol
corresponding to str, creating the symbol if it did not previously exist. See Symbol#id2name
.
"Koala".intern #=> :Koala s = 'cat'.to_sym #=> :cat s == :cat #=> true s = '@cat'.to_sym #=> :@cat s == :@cat #=> true
This can also be used to create symbols that cannot be represented using the :xxx
notation.
'cat and dog'.to_sym #=> :"cat and dog"