Chapter 7 Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Modules

A

Many object-oriented languages provide a way to define a named group of methods that are independent of class and can be mixed in to any object. In Ruby, these mix-ins are called modules.

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

Modules

A

Many object-oriented languages provide a way to define a named group of methods that are independent of class and can be mixed in to any object. In Ruby, these mix-ins are called modules.

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

include

A

Including a module into a class adds the module’s methods to the response set for all instances of that class.

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

Extend

A

it is also possible to add a module’s methods to a single object, using Ruby’s extend keyword. Because extend adds the module’s behavior directly to an object, extending a class with a module creates class methods in that class and extending an instance of a class with a module creates instance methods in that instance.

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

Extend

A

it is also possible to add a module’s methods to a single object, using Ruby’s extend keyword. Because extend adds the module’s behavior directly to an object, extending a class with a module creates class methods in that class and extending an instance of a class with a module creates instance methods in that instance.

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

“Singleton class.”

A

Finally, any object can also have ad hoc methods added directly to its own personal “Singleton class.” These ad hoc methods are unique to this specific object.

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

“Honor The Contract”

A

Subclasses agree to a contract; they promise to be substitutable for their superclasses. Substitutability is possible only when objects behave as expected and subclasses are expected to conform to their superclass’s interface. They must respond to every message in that interface, taking the same kinds of inputs and returning the same kinds of outputs. They are not permitted to do anything that forces others to check their type in order to know how to treat them or what to expect of them.

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

Liskov Substitution Principle (LSP)

A

in order for a type system to be sane, subtypes must be substitutable for their supertypes.

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

Liskov Substitution Principle (LSP)

A

in order for a type system to be sane, subtypes must be substitutable for their supertypes.

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

Chapter 7 Summary

A

When objects that play a common role need to share behavior, they do so via a Ruby module. The code defined in a module can be added to any object, be it an instance of a class, a class itself, or another module.

When a class includes a module, the methods in that module get put into the same lookup path as methods acquired via inheritance. Because module methods and inherited methods interleave in the lookup path, the coding techniques for modules mirror those of inheritance. Modules, therefore, should use the template method pattern to invite those that include them to supply specializations, and should implement hook methods to avoid forcing includers to send super (and thus know the algorithm).

When an object acquires behavior that was defined elsewhere, regardless of whether this elsewhere is a superclass or an included module, the acquiring object makes a commitment to honoring an implied contract. This contract is defined by the Liskov Substitution Principle, which in mathematical terms says that a subtype should be substitutable for its supertype, and in Ruby terms this means that an object should act like what it claims to be.

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