-
Book Overview & Buying
-
Table Of Contents
Polished Ruby Programming - Second Edition
By :
If you come across the following code:
things = ["foo", "bar", "baz"]
things.each do
puts it
end
You probably have an immediate understanding of what it does. If you use a custom class instead:
things = ThingList.new("foo", "bar", "baz")
things.each do
puts it
end
You can probably guess what it does, but to be sure, you need to know about the ThingList class and how it is implemented. What does ThingList.new do? Does it use its arguments directly, or does it wrap them in other objects? What does the ThingList#each method yield? Does it yield the same objects passed into the constructor, or other objects? When you come across code like this, your initial assumption may be that it would yield other objects and not the objects passed into the constructor, because why else would you have a class that duplicates the core Array class?
A good general principle is to only create custom classes when...
Change the font size
Change margin width
Change background colour