Run a business? Check out LessAccounting, our bookkeeping system that'll save you hours per week.

Announcing:

We built the entrepreneur's bookkeeping system, it's called LessAccounting. You'll probably love it.

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:

Business Owners: save hours per week with LessAccounting. It's like Quickbooks, just not total shit.

Leave a Comment

About Steven
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.

You Should...

Follow Steven on Twitter
Friend Steven on Facebook
Subscribe
LessEverything Copyright 2011 LessEverything.com
We don't like footers, they're kinda boring