|
Since it's ruby, there's actually NO reason for Elephant to inherit from node, and all his code would work just the same if it didn't. I think that was basically an error on his part. And, since it's ruby, the answer to "how can I put things other than Elephants in boxes", with the current code example, is "just make sure they have a #num_elephants method". Which, yeah, seems kind of insane, although part of that is the artificial example. An IMO better solution still within this kind of OO world would be to declare an interface of some kind "CountableNode" or something, and say, anything you put in a box has to implement that interface (which to begin with has one method, #num_elephants). There are ways to do that sort of thing in ruby (there are ways to do almost anything in ruby), but they aren't done very often in rubyland, which I do think is a failing in the ruby idiom. Although many rubyists will say they've never found it a problem in practice (maybe they're right, maybe they just didn't recognize the root of the problems they did have). Anyway, you make reasonable points even with these 'corrections', but Fowler could have made his example a bit tighter while still staying firmly within conventional ruby and/or OO best practices. |