1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Array Quick Reference

By

Array Quick Reference

Ruby is not as simple as it once was. With each release, the list of methods grows and grows. For example, the Array class alone has about 90 methods, more if you include the inherited methods and methods from the Enumerable mixin. This can be dizzying. This document cuts through all the extra stuff and just tells you what you need to know for dealing with Arrays.

Creating Arrays

There are two main ways to create Arrays, the Array literal and the Array constructor.

  • The Array literal allows you to create Arrays of a known size and composition. For example, the [1,2,3] array literal creates an array with those three elements. There are also the alternate syntaxes for creating arrays of strings, such as %w[ foo bar baz ].
  • The Array.new constructor is useful for creating Arrays of a specific size (even empty Arrays) with either empty elements (nil), or elements of a default value. For example, Array.new(10) will create an array of 10 elements, each of which are nil. And Array.new(10,'foo') will create an array of 10 elements, each of which are the string 'foo.' However, this is not as common as building arrays programatically (see below).

Array Size

How many elements in the Array? That's easily found out using the size or length methods (they both do the same thing). If you have a = [1,2,3] and you call a.size, it will return 3.

Is an array empty? Call the empty? method, it will return true if the array is empty, otherwise it will return false. This is equivalent to saying a.size == 0, but more semantic.

Indexing Arrays

Once you have an Array, you'll usually want to refer to specific elements. To do this, use the [] operator, just as you would in C, Java, C# or just about any other programming language. If you have a = [1,2,3] then a[0] will return 1 and a[2] will return 3.

You can also refer to Array elements starting from the last element by using negative numbers. Since there is no negative zero, -1 refers to the last element, -2 to the one before that, and so on. If you have a = [1,2,3] then a[-1] will be 3 and a[-2] will be 2.

Iterating Over Arrays

So, you have an array and you need to do something for each element of the Array. This is technically part of the Enumerable mixin module, so look there for detailed information.

  • If you have the array a = [1,2,3] and you call a.each{|e| puts e}, it will print each element of the Array. Note that this is a normal method that just takes a block, this is not a syntax hack. You can also add a with_index enumerator to the mix to get the Array indices as well (something you'll often do in C or Java, but normally not needed in Ruby). If you have a = [1,2,3] and you call a.each.with_index{|e,idx| puts "#{idx}: #{e}" }, it will iterate over the array and provide both array elements and indices to the block.
  • Something very idiomatic to Ruby is to map one array to another. By that, I mean iterate over an Array and produce a new Array based on the elements of the previous Array. If you have a = [1,2,3] and you call b = a.map{|e| e + 10}, the new array stored in the b variable will now be equal to [11,12,13].

Arrays as Stacks

A common data structure is the stack, also know as the FIFO (first in, first out) stack. While Ruby arrays are not true stacks, they can function as stacks.

  • The push method adds a new element to the end of the Array, thus increasing its size by one. If a = [1,2,3] and you do a.push(4), you get the array [1,2,3,4].
  • The pop method removes the last element of the array and returns it. If a = [1,2,3,4] and you call a.pop, that method call will yield the value 4 and the array will now be equal to [1,2,3].

Similarly, the unshift and shift methods do the same, except on the front of the list.

Building Arrays

It's a common operation to start with an empty Array and add on to it. This can be done in a few ways.

  • The aforementioned push method adds on to the end of an array. Slightly more idiomatic is the << operator. If a = [1,2,3] and you call a << 4, the array is now equal to [1,2,3,4].
  • You can also combine two arrays using the + operator. If you have a = [1,2,3]; b = [4,5,6], the statement a + b will yield [1,2,3,4,5,6].

Finding Things in Arrays

You have an array of objects and you need find a specific object, or all objects with a specific property.

  • If you have the Array a = [1,2,3,4,5,6,7,8] and you call a.select{|e| e.odd? }, it will return an Array of all elements in a that are odd, that is it will return [1,3,5,7]. If you just want to find the first element that matches the block, use find instead of select. Another method that does the same thing is the find_all method, but select is more idiomatic.
  • If you want to find the index of the first element that matches a block, use the index method. If a = [1,2,3,4,5,6,7,8], then a.index(3) will return 2 (Array indices start at 0), while a.index{|e| e%4 == 0} will return 3, the index of the first element in the list that is divisible by 4 (in this case, 4 itself).
  1. About.com
  2. Technology
  3. Ruby
  4. Beginning Ruby
  5. Array Quick Reference

©2014 About.com. All rights reserved.