Returns array [self.abs, self.arg]
.
Returns self
.
Raises an exception if the value for freeze
is neither true
nor nil
.
Related: Numeric#dup
.
Returns the absolute value of self
.
12.abs #=> 12 (-34.56).abs #=> 34.56 -34.56.abs #=> 34.56
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 true
if self
is a finite number, false
otherwise.
Returns nil
, -1, or 1 depending on whether self
is finite, -Infinity
, or +Infinity
.
Returns zero.
Returns self
.
Splits str
into an array of tokens in the same way the UNIX Bourne shell does.
See Shellwords.shellsplit
for details.
Extracts data from self
, forming objects that become the elements of a new array; returns that array. See Packed Data.
Like String#unpack
, but unpacks and returns only the first extracted object. See Packed Data.
Inserts the given other_string
into self
; returns self
.
If the Integer
index
is positive, inserts other_string
at offset index
:
'foo'.insert(1, 'bar') # => "fbaroo"
If the Integer
index
is negative, counts backward from the end of self
and inserts other_string
at offset index+1
(that is, after self[index]
):
'foo'.insert(-2, 'bar') # => "fobaro"
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 ocurs just as if field_sep
were given as that string (see below).
When field_sep
is ' '
and limit
is nil
, 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 nil
, 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 nil
, 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 nil
, trailing empty substrings are not returned; the same is true if limit
is zero:
'aaabcdaaa'.split('a') => ["", "", "", "bcd"] 'aaabcdaaa'.split('a', 0) # => ["", "", "", "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 nil
, 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:
'abc def ghi'.split(' ') {|substring| p substring }
Output:
"abc" "def" "ghi"
Related: String#partition
, String#rpartition
.
Returns an array of the characters in self
:
'hello'.chars # => ["h", "e", "l", "l", "o"] 'тест'.chars # => ["т", "е", "с", "т"] 'こんにちは'.chars # => ["こ", "ん", "に", "ち", "は"]
Concatenates each object in objects
to self
and returns self
:
s = 'foo' s.concat('bar', 'baz') # => "foobarbaz" s # => "foobarbaz"
For each given object object
that is an Integer
, the value is considered a codepoint and converted to a character before concatenation:
s = 'foo' s.concat(32, 'bar', 32, 'baz') # => "foo bar baz"
Related: String#<<
, which takes a single argument.
Returns 0 if self
is positive, Math::PI otherwise.
Returns:
1, if self
is Infinity
.
-1 if self
is -Infinity
.
nil
, otherwise.
Examples:
f = 1.0/0.0 # => Infinity f.infinite? # => 1 f = -1.0/0.0 # => -Infinity f.infinite? # => -1 f = 1.0 # => 1.0 f.infinite? # => nil f = 0.0/0.0 # => NaN f.infinite? # => nil
Returns true
if self
is not Infinity
, -Infinity
, or NaN
, false
otherwise:
f = 2.0 # => 2.0 f.finite? # => true f = 1.0/0.0 # => Infinity f.finite? # => false f = -1.0/0.0 # => -Infinity f.finite? # => false f = 0.0/0.0 # => NaN f.finite? # => false
Returns the dirpath
string that was used to create self
(or nil
if created by method Dir.for_fd
):
Dir.new('example').path # => "example"