The coerce method provides support for Ruby type coercion. It is not enabled by default.
This means that binary operations like + * / or - can often be performed on a BigDecimal
and an object of another type, if the other object can be coerced into a BigDecimal
value.
e.g.
a = BigDecimal("1.0") b = a / 2.0 #=> 0.5
Note that coercing a String
to a BigDecimal
is not supported by default; it requires a special compile-time option when building Ruby.
Returns the smallest number greater than or equal to rat
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 a rational when ndigits
is positive, otherwise returns an integer.
Rational(3).ceil #=> 3 Rational(2, 3).ceil #=> 1 Rational(-3, 2).ceil #=> -1 # decimal - 1 2 3 . 4 5 6 # ^ ^ ^ ^ ^ ^ # precision -3 -2 -1 0 +1 +2 Rational('-123.456').ceil(+1).to_f #=> -123.4 Rational('-123.456').ceil(-1) #=> -120
Returns a simpler approximation of the value if the optional argument eps
is given (rat-|eps| <= result <= rat+|eps|), self otherwise.
r = Rational(5033165, 16777216) r.rationalize #=> (5033165/16777216) r.rationalize(Rational('0.01')) #=> (3/10) r.rationalize(Rational('0.1')) #=> (1/3)
Returns true if the date is before the day of calendar reform.
Date.new(1582,10,15).julian? #=> false (Date.new(1582,10,15) - 1).julian? #=> true
This method is equivalent to new_start
(Date::JULIAN
).
Ceils subsecond to a given precision in decimal digits (0 digits by default). It returns a new Time
object. ndigits
should be zero or a positive integer.
t = Time.utc(2010,3,30, 5,43,25.0123456789r) t #=> 2010-03-30 05:43:25 123456789/10000000000 UTC t.ceil #=> 2010-03-30 05:43:26 UTC t.ceil(0) #=> 2010-03-30 05:43:26 UTC t.ceil(1) #=> 2010-03-30 05:43:25.1 UTC t.ceil(2) #=> 2010-03-30 05:43:25.02 UTC t.ceil(3) #=> 2010-03-30 05:43:25.013 UTC t.ceil(4) #=> 2010-03-30 05:43:25.0124 UTC t = Time.utc(1999,12,31, 23,59,59) (t + 0.4).ceil #=> 2000-01-01 00:00:00 UTC (t + 0.9).ceil #=> 2000-01-01 00:00:00 UTC (t + 1.4).ceil #=> 2000-01-01 00:00:01 UTC (t + 1.9).ceil #=> 2000-01-01 00:00:01 UTC t = Time.utc(1999,12,31, 23,59,59) (t + 0.123456789).ceil(4) #=> 1999-12-31 23:59:59.1235 UTC
Replaces the contents of the database with the contents of the specified object. Takes any object which implements the each_pair
method, including Hash
and DBM
objects.
Reads the entire file specified by name as individual lines, and returns those lines in an array. Lines are separated by sep.
a = IO.readlines("testfile") a[0] #=> "This is line one\n" b = IO.readlines("testfile", chomp: true) b[0] #=> "This is line one"
If the last argument is a hash, it’s the keyword argument to open.
The options hash accepts the following keys:
When the optional chomp
keyword argument has a true value, \n
, \r
, and \r\n
will be removed from the end of each line.
See also IO.read
for details about open_args.
Returns the current line number in ios. The stream must be opened for reading. lineno
counts the number of times gets
is called rather than the number of newlines encountered. The two values will differ if gets
is called with a separator other than newline.
Methods that use $/
like each
, lines and readline
will also increment lineno
.
See also the $.
variable.
f = File.new("testfile") f.lineno #=> 0 f.gets #=> "This is line one\n" f.lineno #=> 1 f.gets #=> "This is line two\n" f.lineno #=> 2
Manually sets the current line number to the given value. $.
is updated only on the next read.
f = File.new("testfile") f.gets #=> "This is line one\n" $. #=> 1 f.lineno = 1000 f.lineno #=> 1000 $. #=> 1 # lineno of last read f.gets #=> "This is line two\n" $. #=> 1001 # lineno of last read
Reads all of the lines in ios, and returns them in an array. Lines are separated by the optional sep. If sep is nil
, the rest of the stream is returned as a single record. If the first argument is an integer, or an optional second argument is given, the returning string would not be longer than the given value in bytes. The stream must be opened for reading or an IOError
will be raised.
f = File.new("testfile") f.readlines[0] #=> "This is line one\n" f = File.new("testfile", chomp: true) f.readlines[0] #=> "This is line one"
See IO.readlines
for details about getline_args.
Replaces the content of gdbm with the key-value pairs of other. other must have an each_pair
method.
Returns the original string of the pattern.
/ab+c/ix.source #=> "ab+c"
Note that escape sequences are retained as is.
/\x20\+/.source #=> "\\x20\\+"
Replaces the contents of the set with the contents of the given enumerable object and returns self.
set = Set[1, 'c', :s] #=> #<Set: {1, "c", :s}> set.replace([1, 2]) #=> #<Set: {1, 2}> set #=> #<Set: {1, 2}>
Same as sym.to_s.capitalize.intern
.
Iterates over and yields a new Pathname
object for each element in the given path in descending order.
Pathname.new('/path/to/some/file.rb').descend {|v| p v} #<Pathname:/> #<Pathname:/path> #<Pathname:/path/to> #<Pathname:/path/to/some> #<Pathname:/path/to/some/file.rb> Pathname.new('path/to/some/file.rb').descend {|v| p v} #<Pathname:path> #<Pathname:path/to> #<Pathname:path/to/some> #<Pathname:path/to/some/file.rb>
Returns an Enumerator
if no block was given.
enum = Pathname.new("/usr/bin/ruby").descend # ... do stuff ... enum.each { |e| ... } # yields Pathnames /, /usr, /usr/bin, and /usr/bin/ruby.
It doesn’t access the filesystem.
Iterates over and yields a new Pathname
object for each element in the given path in ascending order.
Pathname.new('/path/to/some/file.rb').ascend {|v| p v} #<Pathname:/path/to/some/file.rb> #<Pathname:/path/to/some> #<Pathname:/path/to> #<Pathname:/path> #<Pathname:/> Pathname.new('path/to/some/file.rb').ascend {|v| p v} #<Pathname:path/to/some/file.rb> #<Pathname:path/to/some> #<Pathname:path/to> #<Pathname:path>
Returns an Enumerator
if no block was given.
enum = Pathname.new("/usr/bin/ruby").ascend # ... do stuff ... enum.each { |e| ... } # yields Pathnames /usr/bin/ruby, /usr/bin, /usr, and /.
It doesn’t access the filesystem.
Removes a file or directory, using File.unlink
if self
is a file, or Dir.unlink
as necessary.