Loads the given certificate_file
Returns a new array containing only those elements from self
that are not found in any of the given other_arrays
; items are compared using eql?
; order from self
is preserved:
[0, 1, 1, 2, 1, 1, 3, 1, 1].difference([1]) # => [0, 2, 3] [0, 1, 2, 3].difference([3, 0], [1, 3]) # => [2] [0, 1, 2].difference([4]) # => [0, 1, 2] [0, 1, 2].difference # => [0, 1, 2]
Returns a copy of self
if no arguments are given.
Related: Array#-
; see also Methods for Combining.
Replaces the elements of self
with the elements of other_array
, which must be an array-convertible object; returns self
:
a = ['a', 'b', 'c'] # => ["a", "b", "c"] a.replace(['d', 'e']) # => ["d", "e"]
Related: see Methods for Assigning.
Returns an array with both a numeric
and a int
represented as Integer
objects or Float
objects.
This is achieved by converting numeric
to an Integer
or a Float
.
A TypeError
is raised if the numeric
is not an Integer
or a Float
type.
(0x3FFFFFFFFFFFFFFF+1).coerce(42) #=> [42, 4611686018427387904]
Returns an integer that is a “ceiling” value for self
, as specified by the given ndigits
, which must be an integer-convertible object.
When self
is zero, returns zero (regardless of the value of ndigits
):
0.ceil(2) # => 0 0.ceil(-2) # => 0
When self
is non-zero and ndigits
is non-negative, returns self
:
555.ceil # => 555 555.ceil(50) # => 555
When self
is non-zero and ndigits
is negative, returns a value based on a computed granularity:
The granularity is 10 ** ndigits.abs
.
The returned value is the smallest multiple of the granularity that is greater than or equal to self
.
Examples with positive self
:
ndigits | Granularity | 1234.ceil(ndigits) |
---|---|---|
-1 | 10 | 1240 |
-2 | 100 | 1300 |
-3 | 1000 | 2000 |
-4 | 10000 | 10000 |
-5 | 100000 | 100000 |
Examples with negative self
:
ndigits | Granularity | -1234.ceil(ndigits) |
---|---|---|
-1 | 10 | -1230 |
-2 | 100 | -1200 |
-3 | 1000 | -1000 |
-4 | 10000 | 0 |
-5 | 100000 | 0 |
Related: Integer#floor
.
Returns the result of division self
by numeric
. rounded up to the nearest integer.
3.ceildiv(3) # => 1 4.ceildiv(3) # => 2 4.ceildiv(-3) # => -1 -4.ceildiv(3) # => -1 -4.ceildiv(-3) # => 2 3.ceildiv(1.2) # => 3
Returns the value as a rational. The optional argument eps
is always ignored.
Returns a Rational
object whose value is exactly or approximately equivalent to that of self.real
.
With no argument epsilon
given, returns a Rational object whose value is exactly equal to that of self.real.rationalize
:
Complex.rect(1, 0).rationalize # => (1/1) Complex.rect(1, Rational(0, 1)).rationalize # => (1/1) Complex.rect(3.14159, 0).rationalize # => (314159/100000)
With argument epsilon
given, returns a Rational object whose value is exactly or approximately equal to that of self.real
to the given precision:
Complex.rect(3.14159, 0).rationalize(0.1) # => (16/5) Complex.rect(3.14159, 0).rationalize(0.01) # => (22/7) Complex.rect(3.14159, 0).rationalize(0.001) # => (201/64) Complex.rect(3.14159, 0).rationalize(0.0001) # => (333/106) Complex.rect(3.14159, 0).rationalize(0.00001) # => (355/113) Complex.rect(3.14159, 0).rationalize(0.000001) # => (7433/2366) Complex.rect(3.14159, 0).rationalize(0.0000001) # => (9208/2931) Complex.rect(3.14159, 0).rationalize(0.00000001) # => (47460/15107) Complex.rect(3.14159, 0).rationalize(0.000000001) # => (76149/24239) Complex.rect(3.14159, 0).rationalize(0.0000000001) # => (314159/100000) Complex.rect(3.14159, 0).rationalize(0.0) # => (3537115888337719/1125899906842624)
Related: Complex#to_r
.
Returns zero as a Rational:
nil.rationalize # => (0/1)
Argument eps
is ignored.
Returns a 2-element array containing two numeric elements, formed from the two operands self
and other
, of a common compatible type.
Of the Core and Standard Library classes, Integer
, Rational
, and Complex
use this implementation.
Examples:
i = 2 # => 2 i.coerce(3) # => [3, 2] i.coerce(3.0) # => [3.0, 2.0] i.coerce(Rational(1, 2)) # => [0.5, 2.0] i.coerce(Complex(3, 4)) # Raises RangeError. r = Rational(5, 2) # => (5/2) r.coerce(2) # => [(2/1), (5/2)] r.coerce(2.0) # => [2.0, 2.5] r.coerce(Rational(2, 3)) # => [(2/3), (5/2)] r.coerce(Complex(3, 4)) # => [(3+4i), ((5/2)+0i)] c = Complex(2, 3) # => (2+3i) c.coerce(2) # => [(2+0i), (2+3i)] c.coerce(2.0) # => [(2.0+0i), (2+3i)] c.coerce(Rational(1, 2)) # => [((1/2)+0i), (2+3i)] c.coerce(Complex(3, 4)) # => [(3+4i), (2+3i)]
Raises an exception if any type conversion fails.
Returns the smallest float or integer that is greater than or equal to self
, as specified by the given ‘ndigits`, which must be an integer-convertible object.
Equivalent to self.to_f.ceil(ndigits)
.
Related: floor
, Float#ceil
.
Splits str
into an array of tokens in the same way the UNIX Bourne shell does.
See Shellwords.shellsplit
for details.
Replaces the contents of self
with the contents of other_string
:
s = 'foo' # => "foo" s.replace('bar') # => "bar"
Returns a string containing the characters in self
; the first character is upcased; the remaining characters are downcased:
s = 'hello World!' # => "hello World!" s.capitalize # => "Hello world!"
The casing may be affected by the given options
; see Case Mapping.
Related: String#capitalize!
.
Upcases the first character in self
; downcases the remaining characters; returns self
if any changes were made, nil
otherwise:
s = 'hello World!' # => "hello World!" s.capitalize! # => "Hello world!" s # => "Hello world!" s.capitalize! # => nil
The casing may be affected by the given options
; see Case Mapping.
Related: String#capitalize
.
Returns an array of substrings of self
that are the result of splitting self
at each occurrence of the given field separator field_sep
.
When field_sep
is $;
:
If $;
is nil
(its default value), the split occurs just as if field_sep
were given as a space character (see below).
If $;
is a string, the split occurs just as if field_sep
were given as that string (see below).
When field_sep
is ' '
and limit
is 0
(its default value), the split occurs at each sequence of whitespace:
'abc def ghi'.split(' ') # => ["abc", "def", "ghi"] "abc \n\tdef\t\n ghi".split(' ') # => ["abc", "def", "ghi"] 'abc def ghi'.split(' ') # => ["abc", "def", "ghi"] ''.split(' ') # => []
When field_sep
is a string different from ' '
and limit
is 0
, the split occurs at each occurrence of field_sep
; trailing empty substrings are not returned:
'abracadabra'.split('ab') # => ["", "racad", "ra"] 'aaabcdaaa'.split('a') # => ["", "", "", "bcd"] ''.split('a') # => [] '3.14159'.split('1') # => ["3.", "4", "59"] '!@#$%^$&*($)_+'.split('$') # => ["!@#", "%^", "&*(", ")_+"] 'тест'.split('т') # => ["", "ес"] 'こんにちは'.split('に') # => ["こん", "ちは"]
When field_sep
is a Regexp
and limit
is 0
, the split occurs at each occurrence of a match; trailing empty substrings are not returned:
'abracadabra'.split(/ab/) # => ["", "racad", "ra"] 'aaabcdaaa'.split(/a/) # => ["", "", "", "bcd"] 'aaabcdaaa'.split(//) # => ["a", "a", "a", "b", "c", "d", "a", "a", "a"] '1 + 1 == 2'.split(/\W+/) # => ["1", "1", "2"]
If the Regexp contains groups, their matches are also included in the returned array:
'1:2:3'.split(/(:)()()/, 2) # => ["1", ":", "", "", "2:3"]
As seen above, if limit
is 0
, trailing empty substrings are not returned:
'aaabcdaaa'.split('a') # => ["", "", "", "bcd"]
If limit
is positive integer n
, no more than n - 1-
splits occur, so that at most n
substrings are returned, and trailing empty substrings are included:
'aaabcdaaa'.split('a', 1) # => ["aaabcdaaa"] 'aaabcdaaa'.split('a', 2) # => ["", "aabcdaaa"] 'aaabcdaaa'.split('a', 5) # => ["", "", "", "bcd", "aa"] 'aaabcdaaa'.split('a', 7) # => ["", "", "", "bcd", "", "", ""] 'aaabcdaaa'.split('a', 8) # => ["", "", "", "bcd", "", "", ""]
Note that if field_sep
is a Regexp containing groups, their matches are in the returned array, but do not count toward the limit.
If limit
is negative, it behaves the same as if limit
was zero, meaning that there is no limit, and trailing empty substrings are included:
'aaabcdaaa'.split('a', -1) # => ["", "", "", "bcd", "", "", ""]
If a block is given, it is called with each substring and returns self
:
'abc def ghi'.split(' ') {|substring| p substring }
Output:
"abc" "def" "ghi" => "abc def ghi"
Note that the above example is functionally the same as calling each after split
and giving the same block. However, the above example has better performance because it avoids the creation of an intermediate array. Also, note the different return values.
'abc def ghi'.split(' ').each {|substring| p substring }
Output:
"abc" "def" "ghi" => ["abc", "def", "ghi"]
Related: String#partition
, String#rpartition
.
Forms substrings (“lines”) of self
according to the given arguments (see String#each_line
for details); returns the lines in an array.
Returns a centered copy of self
.
If integer argument size
is greater than the size (in characters) of self
, returns a new string of length size
that is a copy of self
, centered and padded on both ends with pad_string
:
'hello'.center(10) # => " hello " ' hello'.center(10) # => " hello " 'hello'.center(10, 'ab') # => "abhelloaba" 'тест'.center(10) # => " тест " 'こんにちは'.center(10) # => " こんにちは "
If size
is not greater than the size of self
, returns a copy of self
:
'hello'.center(5) # => "hello" 'hello'.center(1) # => "hello"
Related: String#ljust
, String#rjust
.
Returns a 2-element array containing other
converted to a Float and self
:
f = 3.14 # => 3.14 f.coerce(2) # => [2.0, 3.14] f.coerce(2.0) # => [2.0, 3.14] f.coerce(Rational(1, 2)) # => [0.5, 3.14] f.coerce(Complex(1, 0)) # => [1.0, 3.14]
Raises an exception if a type conversion fails.
Returns a numeric that is a “ceiling” value for self
, as specified by the given ndigits
, which must be an integer-convertible object.
When ndigits
is positive, returns a Float
with ndigits
decimal digits after the decimal point (as available, but no fewer than 1):
f = 12345.6789 f.ceil(1) # => 12345.7 f.ceil(3) # => 12345.679 f.ceil(30) # => 12345.6789 f = -12345.6789 f.ceil(1) # => -12345.6 f.ceil(3) # => -12345.678 f.ceil(30) # => -12345.6789 f = 0.0 f.ceil(1) # => 0.0 f.ceil(100) # => 0.0
When ndigits
is non-positive, returns an Integer
based on a computed granularity:
The granularity is 10 ** ndigits.abs
.
The returned value is the largest multiple of the granularity that is less than or equal to self
.
Examples with positive self
:
ndigits | Granularity | 12345.6789.ceil(ndigits) |
---|---|---|
0 | 1 | 12346 |
-1 | 10 | 12350 |
-2 | 100 | 12400 |
-3 | 1000 | 13000 |
-4 | 10000 | 20000 |
-5 | 100000 | 100000 |
Examples with negative self
:
ndigits | Granularity | -12345.6789.ceil(ndigits) |
---|---|---|
0 | 1 | -12345 |
-1 | 10 | -12340 |
-2 | 100 | -12300 |
-3 | 1000 | -12000 |
-4 | 10000 | -10000 |
-5 | 100000 | 0 |
When self
is zero and ndigits
is non-positive, returns Integer
zero:
0.0.ceil(0) # => 0 0.0.ceil(-1) # => 0 0.0.ceil(-2) # => 0
Note that the limited precision of floating-point arithmetic may lead to surprising results:
(2.1 / 0.7).ceil #=> 4 # Not 3 (because 2.1 / 0.7 # => 3.0000000000000004, not 3.0)
Related: Float#floor
.
Returns a simpler approximation of the value (flt-|eps| <= result <= flt+|eps|). If the optional argument eps
is not given, it will be chosen automatically.
0.3.rationalize #=> (3/10) 1.333.rationalize #=> (1333/1000) 1.333.rationalize(0.01) #=> (4/3)
See also Float#to_r
.
Returns the current execution stack of the fiber. start
, count
and end
allow to select only parts of the backtrace.
def level3 Fiber.yield end def level2 level3 end def level1 level2 end f = Fiber.new { level1 } # It is empty before the fiber started f.backtrace #=> [] f.resume f.backtrace #=> ["test.rb:2:in `yield'", "test.rb:2:in `level3'", "test.rb:6:in `level2'", "test.rb:10:in `level1'", "test.rb:13:in `block in <main>'"] p f.backtrace(1) # start from the item 1 #=> ["test.rb:2:in `level3'", "test.rb:6:in `level2'", "test.rb:10:in `level1'", "test.rb:13:in `block in <main>'"] p f.backtrace(2, 2) # start from item 2, take 2 #=> ["test.rb:6:in `level2'", "test.rb:10:in `level1'"] p f.backtrace(1..3) # take items from 1 to 3 #=> ["test.rb:2:in `level3'", "test.rb:6:in `level2'", "test.rb:10:in `level1'"] f.resume # It is nil after the fiber is finished f.backtrace #=> nil
Returns true if the fiber can still be resumed (or transferred to). After finishing execution of the fiber block this method will always return false
.
Removes the directory at dirpath
from the underlying file system:
Dir.rmdir('foo') # => 0
Raises an exception if the directory is not empty.
Creates a new name for an existing file using a hard link. Will not overwrite new_name if it already exists (raising a subclass of SystemCallError
). Not available on all platforms.
File.link("testfile", ".testfile") #=> 0 IO.readlines(".testfile")[0] #=> "This is line one\n"