Effective Ruby: 48 Specific Ways to Write Better Ruby (Effective Software Development Series)

actual I’ve taken the identify approach out of the individual type and moved it right into a module that is then integrated into the individual category. cases of the buyer category nonetheless reply to the identify approach as you’d count on, yet why? truly the ThingsWithNames module isn’t within the inheritance hierarchy as the superclass of individual remains to be item, or is it? because it seems this can be the place Ruby starts off to mislead you and the place we have to discuss singleton sessions a section extra. if you use the comprise way to combine in a module Ruby is doing whatever very sneaky backstage. It creates a singleton classification and inserts it into the category hierarchy. This nameless and invisible classification is associated with the module so that they percentage example equipment and constants. on the subject of the individual type, while it comprises the ThingsWithNames module, Ruby creates a singleton category and silently inserts it because the superclass of individual. “But calling the superclass process on individual returns item, now not ThingsWithNames,” you are saying. Yep, and for strong cause, the singleton type is nameless and invisible so either the superclass and sophistication equipment pass over it. for this reason a extra exact category hierarchy must contain modules too. And that’s precisely what determine 2-2 comprises. determine 2-2. client classification Inheritance Hierarchy With Modules. As every one module is incorporated right into a category it really is inserted into the hierarchy instantly above the together with type in a final in, first out (LIFO) demeanour. every little thing is attached during the superclass variable like a singly associated record. the internet result's that once Ruby is looking for a style it is going to stopover at every one module in opposite order, such a lot lately integrated first.

Download PDF sample

Rated 4.28 of 5 – based on 15 votes