In Ruby, arrays are ordered, integer-indexed collections of any object.
Array indexing starts at 0
.
A negative index is assumed to be relative to the end of the array β e.g.. an index of -1
indicates the last element of the array, -2
is the next to last element in the array, and so on.
Arrays are normally created using the []
notation.
They can create any different type of object.
array = [1, "two", 3.0]
Elements in an array can be retrieved by their indexes using the #[]
method.
This returns the element at index, or returns a subarray starting at the start index and continuing for a specified length.
Negative indices count backward from the end of the array.
a = [ "a", "b", "c", "d", "e" ]
a[2] #=> "c"
a[6] #=> nil
a[1, 3] #=> [ "b", "c", "d" ]
a[-1] #=> "e"
a[-2] #=> "d"
a[-3, 2] #=> ["c", "d"]
There are lots of useful helper methods on arrays. Here are some of the more common:
fibonacci = [0, 1, 1, 2, 3, 5, 8, 13]
fibonacci.size #=> 8
fibonacci.sum #=> 33
fibonacci.reverse #=> [13, 8, 5, 3, 2, 1, 1, 0]
Rather than using loops to iterate through collections, in Ruby we use enumeration.
Enumeration is the act of stepping through a collection (in this case an array) and performing some action on each object. Enumeration is a key concept in Ruby and is used for things like sorting, grouping, mapping, reducing, and much more.
An enumeration to print each word in an array would look like this:
words = ["the", "cat", "sat"]
words.each do |word|
puts word
end
# Output:
# the
# cat
# sat
In this example, we have called the .each
method on our array and passed in a block that takes one parameter (word
) and prints it out.
We'll look at blocks in much more depth later in the Track, but for now think of them as anonymous functions that can take zero or more arguments.
They can be defined using the do...end
syntax (above), or the {}
syntax (below).
Here are some other examples of array methods that use blocks:
fibonacci = [0, 1, 1, 2, 3, 5, 8, 13]
fibonacci.count { |number| number == 1 } #=> 2
fibonacci.any? { |number| number == 6 } #=> false
fibonacci.select { |number| number.odd? } #=> [1, 1, 3, 5, 13]
fibonacci.all? { |number| number < 20 } #=> true
fibonacci.map { |number| number * 2 } #=> [0, 2, 2, 4, 6, 10, 16, 26]
You're an avid bird watcher that keeps track of how many birds have visited your garden in the last seven days.
You have five tasks, all dealing with the numbers of birds that visited your garden.
For comparison purposes, you always keep a copy of last week's counts nearby, which were: 0, 2, 5, 3, 7, 8 and 4. Implement the BirdCount.last_week
method that returns last week's counts:
BirdCount.last_week
# => [0, 2, 5, 3, 7, 8, 4]
Implement the BirdCount#yesterday
method to return how many birds visited your garden yesterday. The bird counts are ordered by day, with the first element being the count of the oldest day, and the last element being today's count.
birds_per_day = [2, 5, 0, 7, 4, 1]
bird_count = BirdCount.new(birds_per_day)
bird_count.yesterday
# => 4
Implement the BirdCount#total
method to return the total number of birds that have visited your garden:
birds_per_day = [2, 5, 0, 7, 4, 1]
bird_count = BirdCount.new(birds_per_day)
bird_count.total
# => 19
Some days are busier than others. A busy day is one where five or more birds have visited your garden.
Implement the BirdCount#busy_days
method to return the number of busy days:
birds_per_day = [2, 5, 0, 7, 4, 1]
bird_count = BirdCount.new(birds_per_day)
bird_count.busy_days
# => 2
Implement the BirdCount#day_without_birds?
method that returns true
if there was a day at which zero birds visited the garden; otherwise, return false
:
birds_per_day = [2, 5, 0, 7, 4, 1]
bird_count = BirdCount.new(birds_per_day)
bird_count.day_without_birds?
# => true
Sign up to Exercism to learn and master Ruby with 20 concepts, 120 exercises, and real human mentoring, all for free.