w2 misc Flashcards

1
Q

How do you cast in Ruby?

A

CastType(val)

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

What does Array#compact do?

A

Removes all nils.

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

Where should you put a collection of functions with no corresponding data?

A

In a module.

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

In a []= method, is the following argument acceptable:

[2, 2]

A

No; you must call it with Class[[x y]]

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

What can we do if the super version of a function returns something?

A

We can store it in a variable.

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

How far do exceptions travel when raised?

A

They travel up the call stack until they are handled. If they are not, the program exits.

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

If a function will be used in a child class but not in the parent class, how should it be declared in the parent class?

A

It should be declared to raise an error if called in the parent class, then overwritten in the child class with the specific behavior.

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

How can we set up a parent class to access the constants of an inherited class?

A

Use the following syntax in the parent class, then declare the constant in the child class:

self.class::CONSTANT

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

How do we pass an array of two elements to a block, when we want to access each element separately?

A

rows_with_cols_arr.each do |dx, dy|

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

How can we use a ternary operator to toggle a value?

A

(color == :white) ? -1 : 1

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

How can we use the ternary operator to set the value of a variable?

A

color = (color == ‘white’) ? ‘black’ : ‘white’

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

T/F: You can include more than one module in a class.

A

T; only multiple inheritance is invalid.

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

What is one way to catch many types of custom errors?

A

Make one custom error class which all other custom errors inherit from, then rescue the parent custom class (this will catch all inherited exceptions)

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

Explain #each_with_object

A

each_with_object([]) do |(dx, dy), moves|

‘moves’ it the blank array, and the first block argument is the object that each was called on.

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

Make an 8x8 grid

A

a = Array.new(8) { Array.new(8) }

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

How do we set the encoding of a ruby file?

A

Add the following comment to the top of a file:

encoding: utf-8

17
Q

Why is it ok to use the methods of a class inside the definition of another class which requires it?

A

B/C nothing is run when the class is declared; it is only evaluated. As long as everything has been loaded by the time the methods are called, everything will run fine.

18
Q

T/F: You can give #shift and #pop arguments to remove a certain number of consecutive elements from the front or back, respectively.

A

T

19
Q

What does Array#index do?

A

Takes a value as an argument, then returns the first index where that element is located.

20
Q

Explain Array#product

A

Creates a new two element array for every element in the argument array paired with every element in the calling array.

21
Q

What is the difference between the two examples:

arry. push(elems)
arry. push(*elems)

A

Assuming arry and elems are both arrays, the first example will push the entire elems array into arry as a single new element, whereas the splat version will push each element separately into arry, effectively concatenating the two arrays.

22
Q

T/F: Array#delete removes only the first occurrence of the argument element.

A

F; all occurrences of the argument are removed.

23
Q

T/F: You cannot raise an exception and handle it in the same begin-rescue-end block.

A

F; You can raise an error in the begin block and rescue it in the rescue block. You can even simply ‘raise “an argument”’, and capture it as a StandardError in the rescue block:

def get_bet 
    begin 
       print "Bet (bankroll: $#{bankroll}) > " 
      bet = gets.chomp.to_i 
       raise 'not enough money' unless bet  e
      puts e.message
      retry 
    end