Mixed Ruby revision Flashcards

1
Q

Which operator returns a new array containing the elements common to both values, without duplicates?

A

The & operator

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

Which operator returns a new array by joining arrays and removing duplicates?

A

The | operator

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

Which method returns a new array containing the original array elements in reverse order?

A

The reverse method (e.g. array.reverse)

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

Reverse the order of the elements in the array below:

elements = [1, 3, 5, 7, 9]

A

elements.reverse

NB If you use the reverse method without (!) and don’t save it to a new variable, it will just reverse it once and keep the original value.

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

Which 2 methods return the number of elements in an array?

A

array.length or array.size

NB you can also use .count with no arguments passed to it and it will perform the same way as these on arrays. It’s not the same as these two though (.length and .size can be used interchangably) - you can do more with .count by passing it arguments. You also cannot use .count on a string without an argument.

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

Which method returns a new array with the elements sorted?

A

array.sort

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

Which method returns a new array with duplicate values removed from the array?

A

array.uniq

(array.uniq! removes duplicates in place.)

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

Which method safeguards an array, preventing it from being modified?

A

array.freeze

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

Which method returns true if an object is present in an array and false otherwise?

A

array.include?(obj)

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

Which method returns the element with the minimum value in an array?

A

array.min

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

Which method returns the element with the maximum value in an array?

A

array.max

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

How could you iterate over an array of elements and puts each element to the console, with a for loop?

A

array = [“apple”, “dragonfruit”, “plum”]
for x in array
puts “Value: #{x}” # alternatively, puts x
end

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

How would you access the value associated with “Lena” within the following hash?

ages = { “Marge” => 28, “Lena”=> 19, “Hamish” => 42 }

A

ages[“Lena”]

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

Why use symbols instead of strings as hash keys?

A

Using symbols not only saves time when doing comparisons, but also saves memory, because they are only stored once.

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

Create a hash (h) containing a key (name) and its value (“Flora”). Show 2 ways of doing this.

A

h = {name: “Flora”} # This method is shorter!

h = {:name => “Flora”}

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

Which method removes a key-value pair from a hash by key?

A

hash.delete(key)

17
Q

Which method returns the key for a given value in a hash or nil if no matching value is found?

A

hash.key(value)

18
Q

Which method creates a new hash, reversing keys and values from the original hash? i.e., in the new hash, the keys from the original hash become values and values become keys

A

hash.invert

19
Q

Which method returns a new array containing all the keys from a hash?

A

hash.keys

20
Q

Which method returns a new array containing all the values from a hash?

A

hash.values

21
Q

Which method returns the length of a hash as an integer?

A

hash.length

22
Q

Which method mergers two hashes together?

A

hash.merge

e.g. hash_a.merge(hash_b)

23
Q

round down a float and convert it to an Integer,
# so 9.52 becomes 9
def round_down_number(float)
# YOUR CODE HERE
end

A

def round_down_number(float)
float.to_i
end

24
Q

What method allows you to filter an array of objects by selecting those that meet a true or false condition?

A

.select

Remember, it needs a block with a statement that evaluates to true or false, e.g.

array = [1, 2, 3, 4, 5, 6]
array.select {|num| num.even? }

25
Q

Combine .select with another method to filter the array to select every other fruit.

fruits = %w(apple orange banana)
# YOUR CODE HERE
# [“apple”, “banana”]

A

fruits = %w(apple orange banana)
fruits.select.with_index { |word, idx| idx.even? }
# [“apple”, “banana”]

26
Q

What method allows you to filter an array of objects by rejecting something you don’t want to include?

A

.reject

It’s the opposite of .select (you can use .select with a not statement for the same purpose but .reject can help keep your code clear).

27
Q

[1, 3, 5]

Use .reject to filter the following array so that no even objects are included.

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

A

array = [1, 2, 3, 4, 5]
array.reject {|num| num.even? }

28
Q

Combine the .reject method with other methods to filter the following array so that no objects containing a 5 are included.

i.e. if you are given an array of numbers, you should return an array that contains no 5 in any digit of any number.

array = [1, 2, 3, 4, 5, 15, 20, 50]
# [1, 2, 3, 4, 20]

A

array = [1, 2, 3, 4, 5, 15, 20, 50]
no_fives_array= array.reject {|num| num.to_s.include?(“5”)}