Array
8 methodsArrays are ordered, integer-indexed collections of any object.
Array#push(*objects) -> ArrayAppends each given object to self. Returns self (with aliases <<).
Parameters
| Name | Type | Description |
|---|---|---|
| objects | Object | Objects to append. |
Returns
Array
Example
a = [1, 2]
a.push(3)
a.push(4, 5)
# a == [1, 2, 3, 4, 5]
a << 6
# a == [1, 2, 3, 4, 5, 6]Array#pop -> object | nilRemoves and returns the last element of self, or nil if the array is empty.
Returns
Object | nil
Example
a = [1, 2, 3]
x = a.pop # 3
# a == [1, 2]Array#length -> IntegerReturns the number of elements in self. Alias: size.
Returns
Integer
Example
[1, 2, 3].length # 3
[].size # 0Array#each {|item| block} -> ArrayCalls the given block once for each element in self, passing that element as a parameter.
Returns
Array
Example
[1, 2, 3].each { |x| puts x }
# prints:
# 1
# 2
# 3Array#map {|item| block} -> ArrayReturns a new array containing the values returned by the block for each element. Alias: collect.
Returns
Array
Example
[1, 2, 3].map { |x| x * x } # [1, 4, 9]Array#select {|item| block} -> ArrayReturns a new array containing all elements for which the block returns a truthy value.
Returns
Array
Example
[1, 2, 3, 4].select { |x| x.even? } # [2, 4]Array#include?(object) -> boolReturns true if the given object is present in self, false otherwise.
Parameters
| Name | Type | Description |
|---|---|---|
| object | Object | Object to check for. |
Returns
Boolean
Example
[1, 2, 3].include?(2) # true
[1, 2, 3].include?(9) # falseArray#join(separator=$,) -> StringReturns the concatenation of all elements converted to strings, joined by separator.
Parameters
| Name | Type | Description |
|---|---|---|
| separator | String | Separator between elements (default empty). |
Returns
String
Example
["a", "b", "c"].join("-") # "a-b-c"
[1, 2, 3].join(", ") # "1, 2, 3"