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 self has a zero value, false otherwise.
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 <tt>zero?</tt> 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.
Related: zero?
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.
Interprets the leading substring of self as a string of octal digits (with an optional sign) and returns the corresponding number; returns zero if there is no such leading substring:
'123'.oct # => 83 '-377'.oct # => -255 '0377non-numeric'.oct # => 255 'non-numeric'.oct # => 0
If self starts with 0, radix indicators are honored; see Kernel#Integer.
Related: String#hex.
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 self is 0.0, false otherwise.
Forces the fiber to be blocking for the duration of the block. Returns the result of the block.
See the “Non-blocking fibers” section in class docs for details.
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 the root directory of the calling process to that specified in dirpath. The new root directory is used for pathnames beginning with '/'. The root directory is inherited by all children of the calling process.
Only a privileged process may call chroot.
See Linux chroot.
Locks or unlocks file self according to the given locking_constant, a bitwise OR of the values in the table below.
Not available on all platforms.
Returns false if File::LOCK_NB is specified and the operation would have blocked; otherwise returns 0.
| Constant | Lock | Effect |
|---|---|---|
File::LOCK_EX |
Exclusive | Only one process may hold an exclusive lock for self at a time. |
File::LOCK_NB |
Non-blocking | No blocking; may be combined with File::LOCK_SH or File::LOCK_EX using the bitwise OR operator |. |
File::LOCK_SH |
Shared | Multiple processes may each hold a shared lock for self at the same time. |
File::LOCK_UN |
Unlock | Remove an existing lock held by this process. |
Example:
# Update a counter using an exclusive lock. # Don't use File::WRONLY because it truncates the file. File.open('counter', File::RDWR | File::CREAT, 0644) do |f| f.flock(File::LOCK_EX) value = f.read.to_i + 1 f.rewind f.write("#{value}\n") f.flush f.truncate(f.pos) end # Read the counter using a shared lock. File.open('counter', 'r') do |f| f.flock(File::LOCK_SH) f.read end
Returns true if the named file exists and has a zero size.
file_name can be an IO object.
Returns true if filepath points to a socket, false otherwise:
require 'socket' File.socket?(Socket.new(:INET, :STREAM)) # => true File.socket?(File.new('t.txt')) # => false
Returns true if filepath points to a block device, false otherwise:
File.blockdev?('/dev/sda1') # => true File.blockdev?(File.new('t.tmp')) # => false
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:.
Print an argument or list of arguments to the default output stream
cgi = CGI.new cgi.print # default: cgi.print == $DEFAULT_OUTPUT.print
Like Time.utc, except that the returned Time object has the local timezone, not the UTC timezone:
# With seven arguments. Time.local(0, 1, 2, 3, 4, 5, 6) # => 0000-01-02 03:04:05.000006 -0600 # With exactly ten arguments. Time.local(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) # => 0005-04-03 02:01:00 -0600
With no argument given:
Returns self if self is a local time.
Otherwise returns a new Time in the user’s local timezone:
t = Time.utc(2000, 1, 1, 20, 15, 1) # => 2000-01-01 20:15:01 UTC t.localtime # => 2000-01-01 14:15:01 -0600
With argument zone given, returns the new Time object created by converting self to the given time zone:
t = Time.utc(2000, 1, 1, 20, 15, 1) # => 2000-01-01 20:15:01 UTC t.localtime("-09:00") # => 2000-01-01 11:15:01 -0900
For forms of argument zone, see Timezone Specifiers.
Returns a new Time object representing the value of self converted to a given timezone; if zone is nil, the local timezone is used:
t = Time.utc(2000) # => 2000-01-01 00:00:00 UTC t.getlocal # => 1999-12-31 18:00:00 -0600 t.getlocal('+12:00') # => 2000-01-01 12:00:00 +1200
For forms of argument zone, see Timezone Specifiers.
Returns a new Time object whose numeric value is that of self, with its seconds value rounded to precision ndigits:
t = Time.utc(2010, 3, 30, 5, 43, 25.123456789r) t # => 2010-03-30 05:43:25.123456789 UTC t.round # => 2010-03-30 05:43:25 UTC t.round(0) # => 2010-03-30 05:43:25 UTC t.round(1) # => 2010-03-30 05:43:25.1 UTC t.round(2) # => 2010-03-30 05:43:25.12 UTC t.round(3) # => 2010-03-30 05:43:25.123 UTC t.round(4) # => 2010-03-30 05:43:25.1235 UTC t = Time.utc(1999, 12,31, 23, 59, 59) t # => 1999-12-31 23:59:59 UTC (t + 0.4).round # => 1999-12-31 23:59:59 UTC (t + 0.49).round # => 1999-12-31 23:59:59 UTC (t + 0.5).round # => 2000-01-01 00:00:00 UTC (t + 1.4).round # => 2000-01-01 00:00:00 UTC (t + 1.49).round # => 2000-01-01 00:00:00 UTC (t + 1.5).round # => 2000-01-01 00:00:01 UTC
Related: Time#ceil, Time#floor.