w2d2 Flashcards

1
Q

Say we want to have a subclass/child class overwrite a method of the superclass/parent class. How can this be accomplished?

A

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

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

When is it a good idea for classes to use inheritance?

A

The more two classes have in common, the more it pays for them to share a single base class.

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

What are the two ways in which we can call ‘super’

A

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.

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

What is the basic syntax to raise an error?

A

raise “message”

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

How do we raise a specific kind of error?

A

raise ErrorClass.new “message”

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

What does a method do when an exception is raised?

A

Unless it is handled, the exception is thrown and the execution of the method stops.

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

When an exception is raised, how do we stop it from being thrown?

A

Catch it.

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

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
A

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’.

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

Describe what happens when a method raises an unhandled exception.

A

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.

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

When would we use the ‘ensure’ keyword, and how do we use it?

A

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

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

Give one example of using the ‘ensure’ keyword appropriately.

A
f = File.open
begin
  f << a_dangerous_operation
ensure
  # must. close. file.
  f.close
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does the ‘retry’ keyword do, and how is it used?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How can we make an implicit begin statement?

A

Use a return statement without a corresponding begin statement; this will place the begin statement at the start of the method.

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

What is the exception used for generic errors?

A

RuntimeError

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

What is the exception used to signal that an argument to a method is invalid?

A

ArgumentError

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

What is a way to create custom errors?

A

Have them inherit from StandardError:

class EngineStalledError < StandardError
end
17
Q

When would you use more than one rescue statement in a begin-rescue block?

A

When you anticipate different exceptions may arise under different circumstances:

begin
  drive_car
rescue EngineStalledError => e
  puts "Rescued from engine stalled!"
rescue CollisionOccurredError => e
  puts "Rescued from collision!"
ensure
  puts "Car stopped."
end
18
Q

What does “hardened” code refer to?

A

Code that has been proofed to handle all possible exceptions, and ensure that execution continues in spite of these exceptions.

19
Q

T/F: You should try to harden code as you write it.

A

F; It is inefficient and impossible to anticipate all things that can go wrong in a program, so you should write the program first and then go back and error-proof it. Remember YAGNI!

20
Q

What is the difference between an object’s state and its behavior?

A

Its state is its data (IVs), and its behavior is what it can do (methods)

21
Q

What is a rule of thumb when decomposing a problem into objects?

A

Each object should only do 1 thing, at the level of abstraction where you would like to make changes.

22
Q

What is the approximate threshold where a class becomes large and unwieldy?

A

> 125 lines

23
Q

When should you introduce inheritance from a base class?

A

When your code needs it to keep different classes DRY.

24
Q

Why does inheriting from a base class make other methods more useful?

A

If they inherit from the same base class, they can use that base class’s API to interface with the same methods as another class which inherits from that base class.

25
Q

T/F: Instance variables are always private by default.

A

T; they can be made public with methods or attr_accessor

26
Q

When should a method be made private?

A

When the caller runs the risk of tampering with the object in a dangerous way, or to abstract the caller from unnecessary low-level details.

27
Q

When thinking about program maintenance, why is keeping a limited API (many private methods) important?

A

The more objects that rely on another’s methods, the greater the likelihood of multiple points of failure; what you expose will be that much harder to change later.

28
Q

What is a good principle of OO design relating to privacy?

A

Minimize the interfaces between your classes; expose the least possible amount of state and behavior, and have a good reason for every piece of information that you expose.

29
Q

ALGORITHM FOR DEEP DUPING AN ARRAY!

A
def dd_map 
     map { |el| el.is_a?(Array) ? el.dd_map : el } 
   end