Eloquent Ruby by Russ Olsen

Eloquent Ruby by Russ Olsen

Author:Russ Olsen
Language: eng
Format: epub, pdf
Publisher: Pearson Education Limited (US titles)
Published: 2011-03-16T04:00:00+00:00


Now, as we saw in the last chapter, you can access all of these constants using their fully qualified names by saying things like ErrorCode::OK or ErrorCode::Busy. This can get tedious if you are doing it a lot; if so, you can drop the qualifying module name by simply including the ErrorCode module:

Nothing like being able to insert modules into your class hierarchy to make the workday go faster!

Wrapping Up

In this chapter we have seen how mixin modules solve the problem of code that needs to be shared among classes without using up the one alloted superclass. We have also seen how mixin modules work, how they get drafted into service as a kind of phantom superclass when they are included into a host class. We have also seen that the key issue with mixins is the interface between the host class and the mixin module—what methods does the module provide and what does it expect the class to supply?

For many new Ruby programmers, mixin modules can act as a sort of skeleton key to the whole philosophy behind the language, the elegant way that Ruby tries to help programmers get code to execute where it is needed. Because of this, mixin modules pop up somewhere in most real-world Ruby applications. Nor are we done with them. Mixins will make an encore appearance in Chapter 20 when we look at a slick way to add both instance and class methods to a class with a single include. For now, however, we will turn to the code block, another way of getting code to where you need it.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.