w2d2 Flashcards
Say we want to have a subclass/child class overwrite a method of the superclass/parent class. How can this be accomplished?
Use the ‘super’ function to reference the parent’s version:
def initialize(first_name, last_name, super_powers)
super(first_name, last_name)
@super_powers = super_powers
end
When is it a good idea for classes to use inheritance?
The more two classes have in common, the more it pays for them to share a single base class.
What are the two ways in which we can call ‘super’
Using the ‘super’ keyword without arguments will implicitly pass any arguments given to the child version of the function to parent version of the function. Including arguments will only pass those arguments to the parent version of the function.
What is the basic syntax to raise an error?
raise “message”
How do we raise a specific kind of error?
raise ErrorClass.new “message”
What does a method do when an exception is raised?
Unless it is handled, the exception is thrown and the execution of the method stops.
When an exception is raised, how do we stop it from being thrown?
Catch it.
Explain the following code:
begin sqrt(num) rescue ArgumentError => e puts "Couldn't take the square root of #{num}" puts "Error was: #{e.message}" end
First, the sqrt method is run with the variable ‘num’.
Then, an ArgumentError is anticipated with the keyword ‘rescue’. If one is raised then it is saved in the variable ‘e’. We can then retrieve its message and send it to the user without crashing the method execution by retrieving ‘e.message’.
Describe what happens when a method raises an unhandled exception.
The exception “bubbles up” the call stack, with each stack frame having a change to handle (rescue/catch are synonyms) the exception, and if ultimately unhanded the exception is printed to the user and the program exits.
When would we use the ‘ensure’ keyword, and how do we use it?
It is used to execute important code that must be run even if an exception is raised. It is used with a begin-rescue block:
begin
a_dangerous_operation
rescue StandardError => e
puts “Something went wrong: #{e.message}”
ensure
puts “No matter what, make sure to execute this!”
end
Give one example of using the ‘ensure’ keyword appropriately.
f = File.open begin f << a_dangerous_operation ensure # must. close. file. f.close end
What does the ‘retry’ keyword do, and how is it used?
It causes Ruby to loop the begin-rescue block until an exception is no longer raised. It is placed at the end of a rescue statement:
def echo_name # this method uses an implicit begin block fname, lname = prompt_name
puts "Hello #{fname} of #{lname}" rescue puts "Please only use two names." retry end
How can we make an implicit begin statement?
Use a return statement without a corresponding begin statement; this will place the begin statement at the start of the method.
What is the exception used for generic errors?
RuntimeError
What is the exception used to signal that an argument to a method is invalid?
ArgumentError