Why has the InstanceMethods module been deprecated?

Robin picture Robin · Jan 3, 2012 · Viewed 7.8k times · Source

I love ActiveSupport::Concern.

It makes it easy to add functionality to your classes, with a nice syntax.

Anyways, in Rails 3.2, the InstanceMethods module has been deprecated. If I understood correctly, we should just define our methods in the included block (actually it's just in the body of the module):

# edit: don't do this! The method definition should just be in the body of the module
included do
    def my_method; end
end

I was just wondering if anyone knows why they decided to do that?

Answer

Simon Perepelitsa picture Simon Perepelitsa · Jan 3, 2012

Let's look at the example you linked first.

module TagLib
  extend ActiveSupport::Concern

  module ClassMethods
    def find_by_tags()
      # ...
    end
  end

  module InstanceMethods
    def tags()
      # ...
    end
  end 
end

When you include TagLib into your class AS Concern automatically extends the class with ClassMethods module and includes InstanceMethods module.

class Foo
  include TagLib
  # is roughly the same as
  include TagLib::InstanceMethods
  extend TagLib::ClassMethods
end

But as you may noticed we are already including TagLib module itself so the methods defined within it are already available as instance methods on the class. Why would you want to have a separate InstanceMethods module then?

module TagLib
  extend ActiveSupport::Concern

  module ClassMethods
    def find_by_tags()
      # ...
    end
  end

  def tags()
    # ...
  end
end

class Foo
  include TagLib
  # does only `extend TagLib::ClassMethods` for you
end