Returns the first element in self
that is an Array whose first element ==
obj
:
a = [{foo: 0}, [2, 4], [4, 5, 6], [4, 5]] a.assoc(4) # => [4, 5, 6]
Returns nil
if no such element is found.
Related: rassoc
.
Returns the first element in self
that is an Array whose second element ==
obj
:
a = [{foo: 0}, [2, 4], [4, 5, 6], [4, 5]] a.rassoc(4) # => [2, 4]
Returns nil
if no such element is found.
Related: assoc
.
Returns a new Array containing all but the first n
element of self
, where n
is a non-negative Integer; does not modify self
.
Examples:
a = [0, 1, 2, 3, 4, 5] a.drop(0) # => [0, 1, 2, 3, 4, 5] a.drop(1) # => [1, 2, 3, 4, 5] a.drop(2) # => [2, 3, 4, 5]
Prepends the given objects
to self
:
a = [:foo, 'bar', 2] a.unshift(:bam, :bat) # => [:bam, :bat, :foo, "bar", 2]
Array#prepend
is an alias for Array#unshift
.
Returns the predecessor of self
(equivalent to self - 1
):
1.pred #=> 0 -1.pred #=> -2
Related: Integer#succ
(successor value).
Returns self
rounded to the nearest value with a precision of ndigits
decimal digits.
When ndigits
is negative, the returned value has at least ndigits.abs
trailing zeros:
555.round(-1) # => 560 555.round(-2) # => 600 555.round(-3) # => 1000 -555.round(-2) # => -600 555.round(-4) # => 0
Returns self
when ndigits
is zero or positive.
555.round # => 555 555.round(1) # => 555 555.round(50) # => 555
If keyword argument half
is given, and self
is equidistant from the two candidate values, the rounding is according to the given half
value:
:up
or nil
: round away from zero:
25.round(-1, half: :up) # => 30 (-25).round(-1, half: :up) # => -30
:down
: round toward zero:
25.round(-1, half: :down) # => 20 (-25).round(-1, half: :down) # => -20
:even
: round toward the candidate whose last nonzero digit is even:
25.round(-1, half: :even) # => 20 15.round(-1, half: :even) # => 20 (-25).round(-1, half: :even) # => -20
Raises and exception if the value for half
is invalid.
Related: Integer#truncate
.
Returns true
if int
has a zero value.
Returns true
if zero
has a zero value, false
otherwise.
Of the Core and Standard Library classes, only Rational
and Complex
use this implementation.
Returns self
if self
is not a zero value, nil
otherwise; uses method zero?
for the evaluation.
The returned self
allows the method to be chained:
a = %w[z Bb bB bb BB a aA Aa AA A] a.sort {|a, b| (a.downcase <=> b.downcase).nonzero? || a <=> b } # => ["A", "a", "AA", "Aa", "aA", "BB", "Bb", "bB", "bb", "z"]
Of the Core and Standard Library classes, Integer
, Float
, Rational
, and Complex
use this implementation.
Returns self
rounded to the nearest value with a precision of digits
decimal digits.
Numeric implements this by converting self
to a Float
and invoking Float#round
.
Treats leading characters of str as a string of octal digits (with an optional sign) and returns the corresponding number. Returns 0 if the conversion fails.
"123".oct #=> 83 "-377".oct #=> -255 "bad".oct #=> 0 "0377bad".oct #=> 255
If str
starts with 0
, radix indicators are honored. See Kernel#Integer
.
Prepends each string in other_strings
to self
and returns self
:
s = 'foo' s.prepend('bar', 'baz') # => "barbazfoo" s # => "barbazfoo"
Related: String#concat
.
Returns self
rounded to the nearest value with a precision of ndigits
decimal digits.
When ndigits
is non-negative, returns a float with ndigits
after the decimal point (as available):
f = 12345.6789 f.round(1) # => 12345.7 f.round(3) # => 12345.679 f = -12345.6789 f.round(1) # => -12345.7 f.round(3) # => -12345.679
When ndigits
is negative, returns an integer with at least ndigits.abs
trailing zeros:
f = 12345.6789 f.round(0) # => 12346 f.round(-3) # => 12000 f = -12345.6789 f.round(0) # => -12346 f.round(-3) # => -12000
If keyword argument half
is given, and self
is equidistant from the two candidate values, the rounding is according to the given half
value:
:up
or nil
: round away from zero:
2.5.round(half: :up) # => 3 3.5.round(half: :up) # => 4 (-2.5).round(half: :up) # => -3
:down
: round toward zero:
2.5.round(half: :down) # => 2 3.5.round(half: :down) # => 3 (-2.5).round(half: :down) # => -2
:even
: round toward the candidate whose last nonzero digit is even:
2.5.round(half: :even) # => 2 3.5.round(half: :even) # => 4 (-2.5).round(half: :even) # => -2
Raises and exception if the value for half
is invalid.
Related: Float#truncate
.
Returns true
if float
is 0.0.
Returns true
if fiber
is blocking and false
otherwise. Fiber
is non-blocking if it was created via passing blocking: false
to Fiber.new
, or via Fiber.schedule
.
Note that, even if the method returns false
, the fiber behaves differently only if Fiber.scheduler
is set in the current thread.
See the “Non-blocking fibers” section in class docs for details.
Returns false
if the current fiber is non-blocking. Fiber
is non-blocking if it was created via passing blocking: false
to Fiber.new
, or via Fiber.schedule
.
If the current Fiber
is blocking, the method returns 1. Future developments may allow for situations where larger integers could be returned.
Note that, even if the method returns false
, Fiber
behaves differently only if Fiber.scheduler
is set in the current thread.
See the “Non-blocking fibers” section in class docs for details.
Changes this process’s idea of the file system root. Only a privileged process may make this call. Not available on all platforms. On Unix systems, see chroot(2)
for more information.
Locks or unlocks a file according to locking_constant (a logical or of the values in the table below). Returns false
if File::LOCK_NB is specified and the operation would otherwise have blocked. Not available on all platforms.
Locking constants (in class File
):
LOCK_EX | Exclusive lock. Only one process may hold an | exclusive lock for a given file at a time. ----------+------------------------------------------------ LOCK_NB | Don't block when locking. May be combined | with other lock options using logical or. ----------+------------------------------------------------ LOCK_SH | Shared lock. Multiple processes may each hold a | shared lock for a given file at the same time. ----------+------------------------------------------------ LOCK_UN | Unlock.
Example:
# update a counter using write lock # don't use "w" because it truncates the file before lock. File.open("counter", File::RDWR|File::CREAT, 0644) {|f| f.flock(File::LOCK_EX) value = f.read.to_i + 1 f.rewind f.write("#{value}\n") f.flush f.truncate(f.pos) } # read the counter using read lock File.open("counter", "r") {|f| f.flock(File::LOCK_SH) p f.read }
Returns true
if the named file exists and has a zero size.
file_name can be an IO
object.
Invokes Module.prepend_features
on each parameter in reverse order.
The equivalent of included
, but for prepended modules.
module A def self.prepended(mod) puts "#{self} prepended to #{mod}" end end module Enumerable prepend A end # => prints "A prepended to Enumerable"
With no arguments, sets the default visibility for subsequently defined methods to private. With arguments, sets the named methods to have private visibility. String
arguments are converted to symbols. An Array
of Symbols and/or Strings is also accepted. If a single argument is passed, it is returned. If no argument is passed, nil is returned. If multiple arguments are passed, the arguments are returned as an array.
module Mod def a() end def b() end private def c() end private :a end Mod.private_instance_methods #=> [:a, :c]
Note that to show a private method on RDoc
, use :doc:
.
Returns an Array
of two Integer
values that represent platform-dependent internal storage properties.
This method is deprecated and will be removed in the future. Instead, use BigDecimal#n_significant_digits
for obtaining the number of significant digits in scientific notation, and BigDecimal#precision
for obtaining the number of digits in decimal notation.