w1d2 revisions Flashcards

1
Q

When we want to select a certain number of items from an array, what method should we use?

A

Array#select

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

What does #dup do? When is it useful?

A

It is an inherited method from the Object class, and returns a copy of the object. It is useful when we do not want to modify the original object and/or ‘self’

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

What is the shorthand for reassigning values to multiple objects? How can we use it to swap the values of two variables?

A

var1, var2 = new_var1, new_var2

-‘var1’ now has the value of new_var1, var2 now has the value of new_var1

We can swap the values of variables like so:

var1, var2 = var2, var1

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

What would be the code to quickly get an array of lines from a file?

A

lines = File.readlines(‘filename’).map(&:chomp)

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

What is the following syntax short for?

arr.map(&:upcase)

A

arr.map do |elem|
elem.upcase
end

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

T/F: Enumerator#each returns the original object that has been modified by the code block.

A

T

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

As soon as we find ourselves adding a ‘!’ to a condition in an ‘if’ or ‘while’, what should we use instead?

A

‘unless’ or ‘until’, respectively. They are much cleaner.

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

What would be the code to quickly retrieve the first element of each element of a multidimensional array?

A

starts = arr.map(&:first)

USE MAP TO RETURN A NEW ARRAY AND EACH TO EITHER RETURN THE ORIGINAL OBJECT OR THE ORIGINAL OBJECT AFTER IT HAS BEEN MUTATED

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

What is a shorthand to sample a value from a range?

A

rand(start..end)

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

Explain the ‘’ operator

A

It returns -1 if the left-hand operand is less than the right hand operator, 1 if the opposite, and 0 if they are equal.

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

What does File::extname return?

A

The file extension

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

How can File::basename return ONLY the filename and not the extension?

A

File.basename(‘filepath’, “.*”)

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

When should we use Enumerator#any?

A

When we could use #each, but we only want to check if something occurs at least once:

def has_conflict?(new_course)
self.courses.any? do |enrolled_course|
new_course.conflicts_with?(enrolled_course)
end
end

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

How can we raise a generic error?

A

raise “error message” if condition

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

How can we raise a specific class of error?

A

raise ErrorClass.new(“error message”) if condition

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

What is a case statement and when should we use it?

A

Basically a switch statement; declare something and check if it evaluates to each of the when statements:

case (statement)
when value
  actions
when another_value
  actions
else
  default_actions
end
17
Q

What is the method to round numbers correctly (i.e. not always down)

A

Fixnum#fdiv

18
Q

When should we use the ‘=~’ operator?

A

When we are checking to see if a string contains a certain regex.

19
Q

Explain the following code:

Array.new(3) {Array.new(3)}

A

Creates a multidimensional array of 3 by 3

20
Q

T/F: Putting ‘.join’ after the ‘end’ keyword will not do anything.

A

F; it serves to concatenate outputs.

21
Q

What is the difference between:

a, b = [1, 2, 3]

a, *b = [1, 2, 3]

A

In the first example, a = 1 and b = 2

In the second example, a = 1 and b = [2, 3]; the ‘*’ operator here means to catch all remaining elements left over after assignment

22
Q

Why bother with passing |x, y| in the following example:

[arr1, arr2].map do |arrs|
arrs.map{|x, y| newarr[x][y]
end

A

B/C there are two elements in the array passed to the first ‘map’, we need to account for the elements within the second map.

23
Q

Assuming rows, cols and diagonals are multidimensional arrays, what does the following code do?

def winner 
    (rows + cols + diagonals).each do |triple| 
       return :x if triple == [:x, :x, :x] 
       return :o if triple == [:o, :o, :o] 
     end
A

It puts the arrays together into one new array with 3 sub arrays. The each statement grabs the nth element of each sub array and passes them as a new array to ‘triple’