Ruby Arrays Flashcards

1
Q

array

A

An array is an ordered list of elements that can be of any type.

irb :001 > [1, ‘Bob’, 4.33, ‘another string’]

irb :002 > array = [1, ‘Bob’, 4.33, ‘another string’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

pop method

A

If you’d like to take the last item off of an array permanently, you can use the pop method.

irb :007 > array.pop
=> “another string”
irb :008 > array
=> [1, “Bob”, 4.33]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

push method

A

If you’d like to add that item back to the array permanently, you can use the push method and send it the parameters you’d like to add.

irb :009 > array.push(“another string”)
=> [1, “Bob”, 4.33, “another string”]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

shovel operator (<

A

irb :010 > array.pop
=> “another string”
irb :011 > array &laquo_space;“another string”
=> [1, “Bob”, 4.33, “another string”]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

map method

A

The map method iterates over an array applying a block to each element of the array and returns a new array with those results.

irb :001 > a = [1, 2, 3, 4]
=> [1, 2, 3, 4]
irb :002 > a.map { |num| num**2 }
=> [1, 4, 9, 16]

map creates and returns a new array containing the values returned by the block

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

collect method

A

The collect method is an alias to map - they do the same thing.

irb :003 > a.collect { |num| num**2 }
=> [1, 4, 9, 16]
irb :004 > a
=> [1, 2, 3, 4]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

delete_at method

A

eliminate the value at a certain index from your array. Once you call this method, you are changing your array permanently.

irb :005 > a.delete_at(1)
=> 2
irb :006 > a
=> [1, 3, 4]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

delete method

A

sometimes you will know the value that you want to delete, but not the index.

The delete method permanently deletes all instances of the provided value from the array.

irb :007 > my_pets = [“cat”, “dog”, “bird”, “cat”, “snake”]
=> [“cat”, “dog”, “bird”, “cat”, “snake”]
irb :008 > my_pets.delete(“cat”)
=> “cat”
irb :009 > my_pets
=> [“dog”, “bird”, “snake”]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

uniq method

A

his iterates through an array, deletes any duplicate values that exist, then returns the result as a new array.

Once again, notice that the uniq method did not modify the original b array; it returned a new array with the duplicates removed.

irb :010 > b = [1, 1, 2, 2, 3, 3, 4, 4]
=> [1, 1, 2, 2, 3, 3, 4, 4]
irb :011 > b.uniq
=> [1, 2, 3, 4]
irb :012 > b
=> [1, 1, 2, 2, 3, 3, 4, 4]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

uniq!

A

If you add the bang operator (!) to this method, you can perform the uniq function destructively. Much like the way the delete method works.

uniq and uniq! are two different methods for Ruby Arrays. You cannot simply append a ! onto any method and achieve a destructive operation.

irb :013 > b = [1, 1, 2, 2, 3, 3, 4, 4]
=> [1, 1, 2, 2, 3, 3, 4, 4]
irb :014 > b.uniq!
=> [1, 2, 3, 4]
irb :015 > b
=> [1, 2, 3, 4]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

select method

A

This method iterates over an array and returns a new array that includes any items that return true to the expression provided.

irb :001 > numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
irb :002 > numbers.select { |number| number > 4 }
=> [5, 6, 7, 8, 9, 10]
irb :003 > numbers
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

The select method selects all of the numbers that are greater than 4 and returns them in an array. It does not mutate the caller (the original numbers array is unmodified).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

bang operator (!)

A

The bang operator (!) at the end of the method name usually indicates that the method will change (or mutate) the caller permanently. Unfortunately this is not always the case. It is a good rule to be wary of any method that has the bang operator and to make sure to check the Ruby documentation to see if it will behave destructively (the word “destructive” here just means mutating the caller).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

unshift method

A

The unshift method adds the arguments that you specify to the front of the list.

irb :001 > a = [1, 2, 3]
=> [1, 2, 3]
irb :002 > b = [2, 3, 4]
=> [2, 3, 4]
irb :003 > a == b
=> false
irb :004 > b.pop
=> 4
irb :005 > b.unshift(1)
=> [1, 2, 3]
irb :006 > a == b
=> true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

include? method

A

The include? method checks to see if the argument given is included in the array.

irb: 001 > a = [1, 2, 3, 4, 5]
=> [1, 2, 3, 4, 5]
irb: 002 > a.include?(3)
=> true
irb: 003 > a.include?(6)
=> false
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

flatten method

A

The flatten method can be used to take an array that contains nested arrays and create a one-dimensional array.

irb: 001 > a = [1, 2, [3, 4, 5], [6, 7]]
=> [1, 2, [3, 4, 5], [6, 7]]
irb: 002 > a.flatten
=> [1, 2, 3, 4, 5, 6, 7]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

each_index method

A

The each_index method iterates through the array much like the each method, however the variable represents the index number as opposed to the value at each index. It passes the index of the element into the block and you may do as you please with it. The original array is returned.

irb: 001 > a = [1, 2, 3, 4, 5]
=> [1, 2, 3, 4, 5]
irb: 002 > a.each_index { |i| puts "This is index #{i}" }
This is index 0
This is index 1
This is index 2
This is index 3
This is index 4
=> [1, 2, 3, 4, 5]
17
Q

each_with_index

A

Another useful method that works in a similar way to each_index

irb: 001 > a = [1, 2, 3, 4, 5]
=> [1, 2, 3, 4, 5]
irb: 002 > a.each_with_index { |val, idx| puts "#{idx+1}. #{val}" }
1. 1
2. 2
3. 3
4. 4
5. 5
=> [1, 2, 3, 4, 5]

each_with_index gives us the ability to manipulate both the value and the index by passing in two parameters to the block of code. The first is the value and the second is the index. You can then use them in the block.

18
Q

sort method

A

he sort method is a handy way to order an array. It returns a sorted array.

irb :001 > a = [5, 3, 8, 2, 4, 1]
=> [5, 3, 8, 2, 4, 1]
irb :002 > a.sort
=> [1, 2, 3, 4, 5, 8]

19
Q

product method

A

The product method can be used to combine two arrays in an interesting way. It returns an array that is a combination of all elements from all arrays.

irb :001 > [1, 2, 3].product([4, 5])
=> [[1, 4], [1, 5], [2, 4], [2, 5], [3, 4], [3, 5]]

20
Q

.first

A

find the first element of the array

irb :002 > array = [1, ‘Bob’, 4.33, ‘another string’]
irb :003 > array.first
=> 1

21
Q

.last

A

find the last element of the array

irb :002 > array = [1, ‘Bob’, 4.33, ‘another string’]
irb :004 > array.last
=> “another string”

22
Q

indexed lists

A

Arrays are what we call indexed lists.

each slot in an array is numbered.

irb :005 > array[3]