Ruby Array Tips

10 Ruby tips for Array

Ruby have great documentation for Array method for example like this. To day I want to focus not on methods in documentation but some tips I know in Ruby with Array. Let’s start:

  • Create Array with strings:
%w{ 1 2 3 4 }
 => ["1", "2", "3", "4"]
  • Create Array with the same number:
[2]*5
 => [2, 2, 2, 2, 2]
  • Create Array with ordered numbers:
(1..4).to_a
 => [1, 2, 3, 4]
  • Create Array with even numbers:
(2..10).step(2).to_a
 => [2, 4, 6, 8, 10]
  • Show last element in Array:
array = [1, 2, 3, 4]
array[-1]
 => 4
  • Show part of Array:
array = [1, 2, 3, 4]
array[1..2]
 => [2, 3]
array[1...3]
 => [2, 3]
array[1, 2]
  => [2, 3]
  • Array decomposition:
a, b, c = [1, 2, 3, 4]
a => 1
b => 2
c => 3

or

a, b, *c = [1, 2, 3, 4]
a => 1
b => 2
c => [3, 4]
  • Sum all elements in Array:
array = [1, 2, 3, 4]
array.inject(&:+)
 => 10

or

array.reduce(&:+) (for parallel calculations)
 => 10
  • Removed specific elements from Array:
array = [1, 2, 3, 4]
array.slice!(1..2)
array => [1, 4]
  • Array operations:
# Sum
[1, 2, 3] | [1, 4] => [1, 2, 3, 4]
# Concatenation
[1, 2, 3] + [1, 4] => [1, 2, 3, 1, 4]
# Product
[1, 2, 3] & [1, 4] => [1]
# Difference
[1, 2, 3] - [1, 4] => [2, 3]