Collections: Working with Arrays in Ruby

DISCLAIMER: I’m a noob to programming. This is not a guide or something that you should use to learn how to be a programmer. Some of it might be incorrect. This is just for me, John Gamboa, to record my progress in learning Ruby. Now that’s out of the way, on the to the article.

This is an array for a refresh:

array = [1, 2, 3, 3, 4, 5, 5]

Looking for something?

array.first   # returns the first element
              # => 1
array.last    # returns the last element
              # => 5
array[4]      # returns the element in that index (index starts at 0)
              # => 4

Looking for a change? These methods will change the array.

array.pop       # removes last element and mutates
array.push      # add element to the end and it mutates
array << object # "shovel" is the same as push and it mutates

More changes coming.

array.map       # iterates over each element, applies block, returns new array
array.delete_at # deletes value at an index and mutates
array.delete    # deletes all instances of a value and mutates
array.uniq      # returns a new array of all unique values, doesn't mutate
array.uniq!     # mutates array, only unique values are left.

Let me re-iterate with “each” and “select”:

The “each” method iterates through each element of an array (we are still talking arrays here) and applies a block of code to each element.

The “select” method is similar in that it loops through each element but it returns a new array and that new array includes elements that return true to the expression provided. If the return is nil or falsey, then nothing is entered into the array for that element.

select – returns a new array based on the block’s return value. If the return value evaluates to “true” (or checks if it’s truthy) then the element is selected.

Common array methods

array.include?(value) checks if value is in the array.

array.flatten takes an array with nested arrays and turns it into a one-dimensional array.

array.flatten! is flatten that mutates

array.each_index iterates through the index and passes the index of each element into the block.

array.each_with_index iterates through the array passes two parameters into the block, value and index.

array.sort returns a sorted array.

array.product(array2) combines two arrays like this:

[1, 2, 3].product([6, 7]) # => [[1, 6], [1, 7], [2, 6], [2, 7], [3, 6], [3, 7]]

each vs map: use each for iteration and it returns the original array, use map for transformation returns a new array

map – returns a new array based on the block’s return value. Each element is transformed based on the return value.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s