w2 misc Flashcards
How do you cast in Ruby?
CastType(val)
What does Array#compact do?
Removes all nils.
Where should you put a collection of functions with no corresponding data?
In a module.
In a []= method, is the following argument acceptable:
[2, 2]
No; you must call it with Class[[x y]]
What can we do if the super version of a function returns something?
We can store it in a variable.
How far do exceptions travel when raised?
They travel up the call stack until they are handled. If they are not, the program exits.
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?
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 can we set up a parent class to access the constants of an inherited class?
Use the following syntax in the parent class, then declare the constant in the child class:
self.class::CONSTANT
How do we pass an array of two elements to a block, when we want to access each element separately?
rows_with_cols_arr.each do |dx, dy|
How can we use a ternary operator to toggle a value?
(color == :white) ? -1 : 1
How can we use the ternary operator to set the value of a variable?
color = (color == ‘white’) ? ‘black’ : ‘white’
T/F: You can include more than one module in a class.
T; only multiple inheritance is invalid.
What is one way to catch many types of custom errors?
Make one custom error class which all other custom errors inherit from, then rescue the parent custom class (this will catch all inherited exceptions)
Explain #each_with_object
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.
Make an 8x8 grid
a = Array.new(8) { Array.new(8) }
How do we set the encoding of a ruby file?
Add the following comment to the top of a file:
encoding: utf-8
Why is it ok to use the methods of a class inside the definition of another class which requires it?
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.
T/F: You can give #shift and #pop arguments to remove a certain number of consecutive elements from the front or back, respectively.
T
What does Array#index do?
Takes a value as an argument, then returns the first index where that element is located.
Explain Array#product
Creates a new two element array for every element in the argument array paired with every element in the calling array.
What is the difference between the two examples:
arry. push(elems)
arry. push(*elems)
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.
T/F: Array#delete removes only the first occurrence of the argument element.
F; all occurrences of the argument are removed.
T/F: You cannot raise an exception and handle it in the same begin-rescue-end block.
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