When self
is an instance of Array, returns self
:
a = [:foo, 'bar', 2] a.to_a # => [:foo, "bar", 2]
Otherwise, returns a new Array containing the elements of self
:
class MyArray < Array; end a = MyArray.new(['foo', 'bar', 'two']) a.instance_of?(Array) # => false a.kind_of?(Array) # => true a1 = a.to_a a1 # => ["foo", "bar", "two"] a1.class # => Array # Not MyArray
Returns a new Hash
formed from self
.
When a block is given, calls the block with each array element; the block must return a 2-element Array whose two elements form a key-value pair in the returned Hash:
a = ['foo', :bar, 1, [2, 3], {baz: 4}] h = a.to_h {|item| [item, item] } h # => {"foo"=>"foo", :bar=>:bar, 1=>1, [2, 3]=>[2, 3], {:baz=>4}=>{:baz=>4}}
When no block is given, self
must be an Array of 2-element sub-arrays, each sub-array is formed into a key-value pair in the new Hash:
[].to_h # => {} a = [['foo', 'zero'], ['bar', 'one'], ['baz', 'two']] h = a.to_h h # => {"foo"=>"zero", "bar"=>"one", "baz"=>"two"}
Returns a new Array containing only those elements from self
that are not found in any of the Arrays 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]
Returns a copy of self
if no arguments given.
Related: Array#-
.
Returns a new Array with the elements of self
in reverse order:
a = ['foo', 'bar', 'two'] a1 = a.reverse a1 # => ["two", "bar", "foo"]
Reverses self
in place:
a = ['foo', 'bar', 'two'] a.reverse! # => ["two", "bar", "foo"]
Returns a new Array whose elements are those from self
, sorted.
With no block, compares elements using operator <=>
(see Comparable
):
a = 'abcde'.split('').shuffle a # => ["e", "b", "d", "a", "c"] a1 = a.sort a1 # => ["a", "b", "c", "d", "e"]
With a block, calls the block with each element pair; for each element pair a
and b
, the block should return an integer:
Negative when b
is to follow a
.
Zero when a
and b
are equivalent.
Positive when a
is to follow b
.
Example:
a = 'abcde'.split('').shuffle a # => ["e", "b", "d", "a", "c"] a1 = a.sort {|a, b| a <=> b } a1 # => ["a", "b", "c", "d", "e"] a2 = a.sort {|a, b| b <=> a } a2 # => ["e", "d", "c", "b", "a"]
When the block returns zero, the order for a
and b
is indeterminate, and may be unstable:
a = 'abcde'.split('').shuffle a # => ["e", "b", "d", "a", "c"] a1 = a.sort {|a, b| 0 } a1 # => ["c", "e", "b", "d", "a"]
Related: Enumerable#sort_by
.
Returns self
with its elements sorted in place.
With no block, compares elements using operator <=>
(see Comparable
):
a = 'abcde'.split('').shuffle a # => ["e", "b", "d", "a", "c"] a.sort! a # => ["a", "b", "c", "d", "e"]
With a block, calls the block with each element pair; for each element pair a
and b
, the block should return an integer:
Negative when b
is to follow a
.
Zero when a
and b
are equivalent.
Positive when a
is to follow b
.
Example:
a = 'abcde'.split('').shuffle a # => ["e", "b", "d", "a", "c"] a.sort! {|a, b| a <=> b } a # => ["a", "b", "c", "d", "e"] a.sort! {|a, b| b <=> a } a # => ["e", "d", "c", "b", "a"]
When the block returns zero, the order for a
and b
is indeterminate, and may be unstable:
a = 'abcde'.split('').shuffle a # => ["e", "b", "d", "a", "c"] a.sort! {|a, b| 0 } a # => ["d", "e", "c", "a", "b"]
Returns a new Array whose elements are all those from self
for which the block returns false
or nil
:
a = [:foo, 'bar', 2, 'bat'] a1 = a.reject {|element| element.to_s.start_with?('b') } a1 # => [:foo, 2]
Returns a new Enumerator
if no block given:
a = [:foo, 'bar', 2] a.reject # => #<Enumerator: [:foo, "bar", 2]:reject>
Removes each element for which the block returns a truthy value.
Returns self
if any elements removed:
a = [:foo, 'bar', 2, 'bat'] a.reject! {|element| element.to_s.start_with?('b') } # => [:foo, 2]
Returns nil
if no elements removed.
Returns a new Enumerator
if no block given:
a = [:foo, 'bar', 2] a.reject! # => #<Enumerator: [:foo, "bar", 2]:reject!>
Replaces the content of self
with the content of other_array
; returns self
:
a = [:foo, 'bar', 2] a.replace(['foo', :bar, 3]) # => ["foo", :bar, 3]
Returns the new String
formed by calling method #inspect
on each array element:
a = [:foo, 'bar', 2] a.inspect # => "[:foo, \"bar\", 2]"
Prepends the given objects
to self
:
a = [:foo, 'bar', 2] a.unshift(:bam, :bat) # => [:bam, :bat, :foo, "bar", 2]
Returns elements from self
; does not modify self
.
When no argument is given, returns the first element:
a = [:foo, 'bar', 2] a.first # => :foo a # => [:foo, "bar", 2]
If self
is empty, returns nil
.
When non-negative Integer
argument n
is given, returns the first n
elements in a new Array:
a = [:foo, 'bar', 2] a.first(2) # => [:foo, "bar"]
If n >= array.size
, returns all elements:
a = [:foo, 'bar', 2] a.first(50) # => [:foo, "bar", 2]
If n == 0
returns an new empty Array:
a = [:foo, 'bar', 2] a.first(0) # []
Related: last
.
Returns elements from self
; self
is not modified.
When no argument is given, returns the last element:
a = [:foo, 'bar', 2] a.last # => 2 a # => [:foo, "bar", 2]
If self
is empty, returns nil
.
When non-negative Integer
argument n
is given, returns the last n
elements in a new Array:
a = [:foo, 'bar', 2] a.last(2) # => ["bar", 2]
If n >= array.size
, returns all elements:
a = [:foo, 'bar', 2] a.last(50) # => [:foo, "bar", 2]
If n == 0
, returns an new empty Array:
a = [:foo, 'bar', 2] a.last(0) # []
Related: first
.
Calculates the set of unambiguous abbreviations for the strings in self
.
require 'abbrev' %w{ car cone }.abbrev #=> {"car"=>"car", "ca"=>"car", "cone"=>"cone", "con"=>"cone", "co"=>"cone"}
The optional pattern
parameter is a pattern or a string. Only input strings that match the pattern or start with the string are included in the output hash.
%w{ fast boat day }.abbrev(/^.a/) #=> {"fast"=>"fast", "fas"=>"fast", "fa"=>"fast", "day"=>"day", "da"=>"day"} Abbrev.abbrev(%w{car box cone}, "ca") #=> {"car"=>"car", "ca"=>"car"}
See also Abbrev.abbrev
Returns a Hash
containing implementation-dependent counters inside the VM.
This hash includes information about method/constant caches:
{ :constant_cache_invalidations=>2, :constant_cache_misses=>14, :global_cvar_state=>27 }
If USE_DEBUG_COUNTER
is enabled, debug counters will be included.
The contents of the hash are implementation specific and may be changed in the future.
This method is only expected to work on C Ruby.
Returns the value of int
as a BigDecimal
.
require 'bigdecimal' require 'bigdecimal/util' 42.to_d # => 0.42e2
See also Kernel.BigDecimal
.
Returns a string containing the place-value representation of self
in radix base
(in 2..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"
Raises an exception if base
is out of range.
Calls the given block with each integer value from self
up to limit
; returns self
:
a = [] 5.upto(10) {|i| a << i } # => 5 a # => [5, 6, 7, 8, 9, 10] a = [] -5.upto(0) {|i| a << i } # => -5 a # => [-5, -4, -3, -2, -1, 0] 5.upto(4) {|i| fail 'Cannot happen' } # => 5
With no block given, returns an Enumerator
.
Calls the given block with each integer value from self
down to limit
; returns self
:
a = [] 10.downto(5) {|i| a << i } # => 10 a # => [10, 9, 8, 7, 6, 5] a = [] 0.downto(-5) {|i| a << i } # => 0 a # => [0, -1, -2, -3, -4, -5] 4.downto(5) {|i| fail 'Cannot happen' } # => 4
With no block given, returns an Enumerator
.
Returns the predecessor of self
(equivalent to self - 1
):
1.pred #=> 0 -1.pred #=> -2
Related: Integer#succ
(successor value).
Converts self
to a Float:
1.to_f # => 1.0 -1.to_f # => -1.0
If the value of self
does not fit in a Float
, the result is infinity:
(10**400).to_f # => Infinity (-10**400).to_f # => -Infinity
Returns the largest number less than or equal to self
with a precision of ndigits
decimal digits.
When ndigits
is negative, the returned value has at least ndigits.abs
trailing zeros:
555.floor(-1) # => 550 555.floor(-2) # => 500 -555.floor(-2) # => -600 555.floor(-3) # => 0
Returns self
when ndigits
is zero or positive.
555.floor # => 555 555.floor(50) # => 555
Related: Integer#ceil
.