LANGUAGE » RUBY

Array

Basic

ruby
my_array = Array.new
my_array = [1, 2, 3]
my_array = (1..10).to_a
my_words = %w[foo bar]  # %W if going to use string interpolation

my_array[1]
my_array.push(40)
my_array << 40  # Same as push

Methods

MethodDescription
pushAppends each argument to the array.
maxReturns the biggest element in the array.
minReturns the smallest element in the array
include?Returns true if the array includes the specified element.
empty?Returns true if the length of the array is 0.
sortReturns a new array sorted.
sort_by!Sort in place by using the given block.
reverseReturns a new array in inverse order.
joinReturn a string which is the concatenation of the elements.
eachIterate over the elements of the array.
mapReturns a new array with each element updated by the given block.
collectSame as map.

Check if array includes an element:

ruby
my_array.include? 'critical'

Iterate array elements:

ruby
array.each do |element|
  puts element
end

Update elements order:

ruby
my_array.sort! do |v1, v2|
  v1 <=> v2
end
my_array = my_array.sort_by do |key, value|
  value
end
my_array.reverse!

Join elements using \n:

ruby
my_array.join("\n")

Calculate the square of every element:

ruby
my_nums.map { |num| num ** 2 }
my_nums.collect { |num| num ** 2 }