Inserts given objects
before or after the element at Integer index offset
; returns self
.
When index
is non-negative, inserts all given objects
before the element at offset index
:
a = [:foo, 'bar', 2] a.insert(1, :bat, :bam) # => [:foo, :bat, :bam, "bar", 2]
Extends the array if index
is beyond the array (index >= self.size
):
a = [:foo, 'bar', 2] a.insert(5, :bat, :bam) a # => [:foo, "bar", 2, nil, nil, :bat, :bam]
Does nothing if no objects given:
a = [:foo, 'bar', 2] a.insert(1) a.insert(50) a.insert(-50) a # => [:foo, "bar", 2]
When index
is negative, inserts all given objects
after the element at offset index+self.size
:
a = [:foo, 'bar', 2] a.insert(-2, :bat, :bam) a # => [:foo, "bar", :bat, :bam, 2]
Returns the index of a specified element.
When argument object
is given but no block, returns the index of the first element element
for which object == element
:
a = [:foo, 'bar', 2, 'bar'] a.index('bar') # => 1
Returns nil
if no such element found.
When both argument object
and a block are given, calls the block with each successive element; returns the index of the first element for which the block returns a truthy value:
a = [:foo, 'bar', 2, 'bar'] a.index {|element| element == 'bar' } # => 1
Returns nil
if the block never returns a truthy value.
When neither an argument nor a block is given, returns a new Enumerator:
a = [:foo, 'bar', 2] e = a.index e # => #<Enumerator: [:foo, "bar", 2]:index> e.each {|element| element == 'bar' } # => 1
Array#find_index
is an alias for Array#index
.
Related: rindex
.
Returns the index of the last element for which object == element
.
When argument object
is given but no block, returns the index of the last such element found:
a = [:foo, 'bar', 2, 'bar'] a.rindex('bar') # => 3
Returns nil
if no such object found.
When a block is given but no argument, calls the block with each successive element; returns the index of the last element for which the block returns a truthy value:
a = [:foo, 'bar', 2, 'bar'] a.rindex {|element| element == 'bar' } # => 3
Returns nil
if the block never returns a truthy value.
When neither an argument nor a block is given, returns a new Enumerator:
a = [:foo, 'bar', 2, 'bar'] e = a.rindex e # => #<Enumerator: [:foo, "bar", 2, "bar"]:rindex> e.each {|element| element == 'bar' } # => 3
Related: index
.
Returns the new String formed by joining the array elements after conversion. For each element element
Uses element.to_s
if element
is not a kind_of?(Array)
.
Uses recursive element.join(separator)
if element
is a kind_of?(Array)
.
With no argument, joins using the output field separator, $,
:
a = [:foo, 'bar', 2] $, # => nil a.join # => "foobar2"
With string argument separator
, joins using that separator:
a = [:foo, 'bar', 2] a.join("\n") # => "foo\nbar\n2"
Joins recursively for nested Arrays:
a = [:foo, [:bar, [:baz, :bat]]] a.join # => "foobarbazbat"
Calls the block, if given, with each element of self
; returns a new Array whose elements are the return values from the block:
a = [:foo, 'bar', 2] a1 = a.map {|element| element.class } a1 # => [Symbol, String, Integer]
Returns a new Enumerator if no block given:
a = [:foo, 'bar', 2] a1 = a.map a1 # => #<Enumerator: [:foo, "bar", 2]:map>
Array#collect
is an alias for Array#map
.
Calls the block, if given, with each element; replaces the element with the block’s return value:
a = [:foo, 'bar', 2] a.map! { |element| element.class } # => [Symbol, String, Integer]
Returns a new Enumerator if no block given:
a = [:foo, 'bar', 2] a1 = a.map! a1 # => #<Enumerator: [:foo, "bar", 2]:map!>
Array#collect!
is an alias for Array#map!
.
Returns true
if for some index i
in self
, obj == self[i]
; otherwise false
:
[0, 1, 2].include?(2) # => true [0, 1, 2].include?(3) # => false
Calls the block, if given, with combinations of elements of self
; returns self
. The order of combinations is indeterminate.
When a block and an in-range positive Integer argument n
(0 < n <= self.size
) are given, calls the block with all n
-tuple combinations of self
.
Example:
a = [0, 1, 2] a.combination(2) {|combination| p combination }
Output:
[0, 1] [0, 2] [1, 2]
Another example:
a = [0, 1, 2] a.combination(3) {|combination| p combination }
Output:
[0, 1, 2]
When n
is zero, calls the block once with a new empty Array:
a = [0, 1, 2] a1 = a.combination(0) {|combination| p combination }
Output:
[]
When n
is out of range (negative or larger than self.size
), does not call the block:
a = [0, 1, 2] a.combination(-1) {|combination| fail 'Cannot happen' } a.combination(4) {|combination| fail 'Cannot happen' }
Returns a new Enumerator if no block given:
a = [0, 1, 2] a.combination(2) # => #<Enumerator: [0, 1, 2]:combination(2)>
Builds a command line string from an argument list array
joining all elements escaped for the Bourne shell and separated by a space.
See Shellwords.shelljoin
for details.
Returns object. This method is deprecated and will be removed in Ruby 3.2.
Returns false. This method is deprecated and will be removed in Ruby 3.2.
Returns object. This method is deprecated and will be removed in Ruby 3.2.
Returns a string containing a human-readable representation of obj. The default inspect
shows the object’s class name, an encoding of its memory address, and a list of the instance variables and their values (by calling inspect
on each of them). User defined classes should override this method to provide a better representation of obj. When overriding this method, it should return a string whose encoding is compatible with the default external encoding.
[ 1, 2, 3..4, 'five' ].inspect #=> "[1, 2, 3..4, \"five\"]" Time.new.inspect #=> "2008-03-08 19:43:39 +0900" class Foo end Foo.new.inspect #=> "#<Foo:0x0300c868>" class Bar def initialize @bar = 1 end end Bar.new.inspect #=> "#<Bar:0x0300c868 @bar=1>"
Since int
is already an Integer
, this always returns true
.
Returns a string containing the place-value representation of int
with radix base
(between 2 and 36).
12345.to_s #=> "12345" 12345.to_s(2) #=> "11000000111001" 12345.to_s(8) #=> "30071" 12345.to_s(10) #=> "12345" 12345.to_s(16) #=> "3039" 12345.to_s(36) #=> "9ix" 78546939656932.to_s(36) #=> "rubyrules"
Returns the absolute part of its polar form.
Complex(-1).abs #=> 1 Complex(3.0, -4.0).abs #=> 5.0
Returns the value as a string for inspection.
Complex(2).inspect #=> "(2+0i)" Complex('-8/6').inspect #=> "((-4/3)+0i)" Complex('1/2i').inspect #=> "(0+(1/2)*i)" Complex(0, Float::INFINITY).inspect #=> "(0+Infinity*i)" Complex(Float::NAN, Float::NAN).inspect #=> "(NaN+NaN*i)"
Returns true
if cmp
‘s real and imaginary parts are both finite numbers, otherwise returns false
.
Always returns the string “nil”.
Returns zero.
Returns the absolute value of num
.
12.abs #=> 12 (-34.56).abs #=> 34.56 -34.56.abs #=> 34.56
Numeric#magnitude
is an alias for Numeric#abs
.
Returns true
if num
is a finite number, otherwise returns false
.