Arrays Flashcards

1
Q

array

A

an ordered list of elements that can be of any type.

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

First method

A

This finds the first element of an array. Array.first

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

Last method

A

This finds the last element of an array. Array.last

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

How do you reference any part of an array by its index number?

A

Array[3]

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

Pop method

A

Takes the last item off an array permanently

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

Push method

A

Add an item back to array permanently. Array.push or &laquo_space;aka the shovel operator

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

Map method

A

Iterates over an array applying a block to each element of an array and returns a new array with those results. Array.map

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

Delete_at method

A

Eliminates the value at the index of the number passed to the method. Modifies array destructively. Array.delete_at

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

Delete method

A

Sometimes I will know the value I want to delete but not the index. This method permanently deletes all instances of this value from the array.

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

Uniq method

A

This method iterates through an array, deletes any duplicates that exist, then returns the result as a new array. Array.uniq

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

Uniq!

A

Destructive method of uniq

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
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. numbers.select { |number| number > 4 }
It does not mutate the caller.

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

unshift method

A

Method that adds values to the start of the list in an array.

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

to_s method

A

Used to create a string representation of an array. Ruby does this behind the scenes when you use string interpolation on an array.

irb :001 > a = [1, 2, 3]
=> [1, 2, 3]
irb :002 > “It’s as easy as #{a}”
=> “It’s as easy as [1, 2, 3]”

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

include? method

A

Checks to see if the argument given is included in the array. It has a question mark in it which usually means you can expect it to return a Boolean true or false.

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
16
Q

flatten method

A

This method can be used to take an array that contains nested arrays and create a one-dimensional array. Only destructive if it includes !

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]

17
Q

each_index method

A

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.

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]
18
Q

each _with_index method

A

Gives us the ability to manipulate both the value and the index by passing in two parameters into the block of code. The first is the value and the second is the 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]
19
Q

sort method

A

This sorts an array by integer value.
Only destructive when followed by !

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]

20
Q

product method

A

Returns an array that is a combination of all the elements from all arrays.

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