Making Methods Available to Class and Instance in Ruby
written by Steven on August 06, 2012
A few days ago I was contrasting extend and include in Ruby. In that post I said I would show you how to make a method available to both the class scope and the instance scope. You can do it by using the included and extend methods:
Module#included is called anytime the module is included in a class. When it is, we just call class.extend on the class that included the module and pass in self (which is the module).
Most people think just using the class method is fine, e.g. Rails.logger, but I like having a method available in both places. It makes me feel warm and fuzzy, like a blanket wrapped around me on a cold winter's night…. Obviously you shouldn't use this technique all the time, only when it makes sense.
Here is a nice general pattern for making modules:
Leave a Comment

Steven Bristol has written code for the past 20 years. He like green vegetables and kittens, oh and butterflies too. He loves to throw ninja stars at his enemies.
