Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why has the InstanceMethods module been deprecated?

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?

like image 229
Robin Avatar asked Jan 03 '12 01:01

Robin


1 Answers

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
like image 139
Simon Perepelitsa Avatar answered Nov 11 '22 21:11

Simon Perepelitsa