Arrays

Array literals

Square brackets build an array, with elements separated by commas. Elements may mix types, because an array holds references to objects, not fixed slots. The factory method Array.new builds a sized array filled with one default value.

nums = [1, 2, 3] mixed = ["Ada", 36, true] zeros = Array.new(3, 0) puts nums.length puts mixed.length puts zeros.length

ruby zeros.rb

3
3
3

The last line prints 3, because zeros holds three copies of 0.

Read and write by index

Read an element with brackets and its position, starting at 0 for the first element. A negative index counts back from the end, so -1 is the last element. Reading a missing position returns nil, while assigning to a position replaces the value there.

names = ["Ada", "Bob", "Lin"] puts names[0] puts names[-1] names[1] = "Boris" puts names[1]

ruby names.rb

Ada
Lin
Boris

Push pop shift

The method push appends to the end, pop removes the last element and returns it, and shift removes the first element and returns it. The method length, with its alias size, tells how many elements are stored.

stack = [1, 2] stack.push(3) puts stack.length last = stack.pop puts last puts stack.length

ruby stack.rb

3
3
2

Iterate with each

The method each hands every element to the block in order. When the position matters as well, each_with_index yields the element together with its index, starting at 0.

names = ["Ada", "Bob", "Lin"] names.each do |name| puts name end names.each_with_index do |name, i| puts "#{i}: #{name}" end

ruby each.rb

Ada
Bob
Lin
0: Ada
1: Bob
2: Lin

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. How is an array written in Ruby?

    Answer

    [1, 2, 3]

    Square brackets with commas build an array; braces build a hash and parentheses group an expression.

  2. What does Array.new(3, 0) build?

    Answer

    An array of three zeros

    The first argument is the size and the second the value each slot holds.

  3. What does this program print? names = ["Ada", "Bob", "Lin"] puts names[-1]

    Answer

    Lin

    A negative index counts back from the end, so -1 is the last element.

  4. What does reading a missing index give?

    Answer

    nil

    A position past the end returns nil instead of raising.

  5. What does stack.pop do?

    Answer

    Removes and returns the last element

    pop takes the end, shift takes the front, and push appends.

  6. What does this program print? stack = [1, 2] stack.push(3) puts stack.length

    Answer

    3

    push adds one element, so the array holds three.

  7. Which method gives the element and its position to the block?

    Answer

    each_with_index

    each_with_index yields the element and then its index, starting at 0.

  8. What does this program print? names = ["Ada", "Bob"] names.each_with_index { |name, i| puts "#{i}: #{name}" }

    Answer

    0: Ada / 1: Bob

    The block prints the index before the name, one line per element.

  9. What does puts [1, 2, 3].size print?

    Answer

    3

    size is an alias of length and counts the elements.

  10. Why may an array hold a string, a number and true together?

    Answer

    Because the array stores references to objects, not fixed slots

    An array is a list of object references, so the types may differ.

Article author: Arthur Isaev