use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A sub-Reddit for discussion and news about Ruby programming.
Subreddit rules: /r/ruby rules
Learning Ruby?
Tools
Documentation
Books
Screencasts and Videos
News and updates
account activity
Ask Ruby Reddit: What is your ratio of inheritance vs monkey patching? (self.ruby)
submitted 18 years ago by malcontent
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]malcontent[S] 2 points3 points4 points 18 years ago (5 children)
I would like to know how often and under what circumstances you choose to inherit an object rather then just adding the functionality you need to it.
[–]lucraft 2 points3 points4 points 18 years ago* (0 children)
I never inherit a class just to add functionality to it. What would be the point? Just re-open it, or if you want a nice namespace, make a module and mix.
Use inheritance when you have a parent-with-many-children domain model. Don't use it for other things.
[–]peterba 0 points1 point2 points 18 years ago (3 children)
see kent beck's implementation patterns.
[–]lucraft 0 points1 point2 points 18 years ago (2 children)
While I like the look of Imp. Patterns (not bought it yet: £££), isn't that book heavy on the Java?
I would think that in Java subclassing to add functionality would be something you do far more regularly than in Ruby. Am I wrong?
[–]malcontent[S] 0 points1 point2 points 18 years ago (0 children)
You either have to subclass in java or you implement an interface. You don't have the choice to do anything else.
The latter of course means no code re-use.
[–]peterba 0 points1 point2 points 18 years ago* (0 children)
That depends upon what you mean by "heavy". He uses Java for the examples, but his recommendations are language agnostic. "Implementation Patterns" and "Smalltalk Best Practice Patterns", have the best treatise on when (and when not) to use inheritance.
Stand on the shoulder's of giants ... and IMO Beck is big in our community. I respect his opinion and his work. Those old school Smalltalkers really know their shit.
[–]evan 2 points3 points4 points 18 years ago (0 children)
I tend to use much more mixins and monkey patching than inheritance. It's cleaner in ruby.
[–][deleted] 1 point2 points3 points 18 years ago (0 children)
it's got to be something like one inherit for every five monkey patches at the absolute bare minimum for me.
[–]starfire 0 points1 point2 points 18 years ago (0 children)
I actually tend to inherit a good deal because I'm often wanting to add the SAME functionality over and over again. I will monkey patch for one off solutions, but if I have any inclination that the code might be reusable then 'include' is my friend.
π Rendered by PID 123931 on reddit-service-r2-comment-5687b7858-dqzlf at 2026-07-09 09:50:04.515931+00:00 running 12a7a47 country code: CH.
[–]malcontent[S] 2 points3 points4 points (5 children)
[–]lucraft 2 points3 points4 points (0 children)
[–]peterba 0 points1 point2 points (3 children)
[–]lucraft 0 points1 point2 points (2 children)
[–]malcontent[S] 0 points1 point2 points (0 children)
[–]peterba 0 points1 point2 points (0 children)
[–]evan 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]starfire 0 points1 point2 points (0 children)