arrays Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

How do I take the last item off any array?

A

array.pop

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

How do I add an item onto an array?

A

array.push or array &laquo_space;“another string”

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

How do I remove an item from an array?

A
  • a.delete_at(1) … at index 1

- delete(1) …. delete the number 1

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

Which method iterates through an array and deletes any duplicates? Is it destructive?

A

array. uniq

array. uniq!

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

b = [2, 3, 4]
b.pop
What is the return of b.pop?

A

4

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

How do I add a value to the front of a list?

A

array.unshift(1)

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

How do I convert a nested array and create a one dimensional array?

A

array.flatten

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

What method do I use to see if the argument given is in the array?

A

array.include?(4)

it returns a boolean, true or false

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

What does each_index do?

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]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does each_with_index do?

A

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.

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]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do I order an array? Is it destructive?

A

array.sort

it is a non-destructive method.

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

What does the product method do?

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

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

When no argument is given, what method returns the last element?

A

a.last

example:
a = [:foo, ‘bar’, 2]
a.last(2) # => [“bar”, 2]

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

When no argument is given, what method returns the first element?

A

a.first

example:
a = [:foo, ‘bar’, 2]
a.first(2) # => [:foo, “bar”]

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

What does .join do?

A

Array#join returns a string with the elements of the array joined together.

example:
str = ‘How do you get to Carnegie Hall?’
arr.join # => “HowdoyougettoCarnegieHall?”

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

What does .split do?

A

str = ‘How do you get to Carnegie Hall?’

arr = str.split # => [“How”, “do”, “you”, “get”, “to”, “Carnegie”, “Hall?”]

17
Q

What does .chars do?

A

String#chars returns an array of individual characters.

example:
str = ‘Practice’
arr = str.chars # => [“P”, “r”, “a”, “c”, “t”, “i”, “c”, “e”]

18
Q

.sort

A

As we have already seen, we can simply call sort on an array, which returns a new array of ordered items; when we do this, comparisons are carried out using the <=> method on the items being sorted.

We can also call sort with a block; this gives us more control over how the items are sorted. The block needs two arguments passed to it (the two items to be compared) and the return value of the block has to be -1, 0, 1 or nil.

[2, 5, 3, 4, 1].sort do |a, b|
a <=> b
end
# => [1, 2, 3, 4, 5]

19
Q

.sort_by

A

sort_by is similar to sort but is usually called with a block. The code in the block determines how the items are compared.

[‘cot’, ‘bed’, ‘mat’].sort_by do |word|
word[1]
end
# => [“mat”, “bed”, “cot”]

Here we are sorting using the character at index 1 of each string, so only the characters ‘a’, ‘e’ and ‘o’ are compared and the strings ordered according to the comparison of those characters. The other characters in the strings are ignored entirely.

There generally isn’t a need to sort hashes, since hash values are accessed via their keys rather than position. If you did want to sort a hash however, calling sort_by on it would be a way to do so.

When calling sort_by on a hash, two arguments need to be passed to the block - the key and the value. In the people hash in the following example, the keys are each person’s name and the values are their ages.

people = { Kate: 27, john: 25, Mike: 18 }

The last argument evaluated in the block should then be the thing by which we want to sort, so if we wanted the hash sorted by age we could do the following:

people.sort_by do |_, age|
age
end
# => [[:Mike, 18], [:john, 25], [:Kate, 27]]
sort_by always returns an array, even when called on a hash, so the result here is a new array with the key-value pairs as objects in nested arrays. If we need to convert this back into a hash we can call Array#to_h on it.

20
Q

study .freeze, .dup, .clone

A

Lesson 5, Nested Data Structures

21
Q

.concat

A

Adds to array all elements from each Array in other_arrays; returns self:

a = [0, 1]
a.concat([2, 3], [4, 5]) # => [0, 1, 2, 3, 4, 5]

22
Q

.delete, .delete_at

A

.delete removes element from array, returns deleted item.

example:
numbers = [1, 2, 3, 4, 5]
numbers.delete(1) # numbers is now [2, 3, 4, 5]

.delete_at(index) → deleted_object or nil

Deletes an element from self, per the given Integer index.

When index is non-negative, deletes the element at offset index:

a = [:foo, ‘bar’, 2]
a.delete_at(1) # => “bar”
a # => [:foo, 2]

If index is too large, returns nil.

When index is negative, counts backward from the end of the array:

a = [:foo, ‘bar’, 2]
a.delete_at(-2) # => “bar”
a # => [:foo, 2]

return value is the removed item

23
Q

.join

A

a = [:foo, [:bar, [:baz, :bat]]]
a.join => “foobarbazbat”

a = [:foo, ‘bar’, 2]
a.join(“\n”) # => “foo\nbar\n2”

a = [:foo, ‘bar’, 2]
a.join # => “foobar2”

24
Q

none?

A

Returns true if no element of self meet a given criterion.

With no block given and no argument, returns true if self has no truthy elements, false otherwise:

[nil, false].none? # => true
[nil, 0, false].none? # => false
[].none? # => true

With a block given and no argument, calls the block with each element in self; returns true if the block returns no truthy value, false otherwise:

[0, 1, 2].none? {|element| element > 3 } # => true
[0, 1, 2].none? {|element| element > 1 } # => false

If argument obj is given, returns true if obj.=== no element, false otherwise:

['food', 'drink'].none?(/bar/) # => true
['food', 'drink'].none?(/foo/) # => false
[].none?(/foo/) # => true
[0, 1, 2].none?(3) # => true
[0, 1, 2].none?(1) # => false