Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overloading a method in an ActiveSupport::Concern

How can I have a concern that I've written like this:

module Concerns
  module MyConcern
    extend ActiveSupport::Concern
    ...
    def my_concern_magic(arg0,arg1)
      #exciting stuff here
    end
  end 
end 

that is included in a model that overloads my_concern_magic? E.g.

class User
  include Concerns::MyConcern
  ...
  def my_concern_magic(arg0)
    arg1 = [1,2,3]
    my_concern_magic(arg0,arg1)
  end
end
like image 929
Jon Lebensold Avatar asked Dec 28 '12 22:12

Jon Lebensold


People also ask

How do you differentiate overloaded methods from overloaded methods?

Overloaded methods are differentiated based on the number and type of the parameters passed as arguments to the methods. You can not define more than one method with the same name, Order and the type of the arguments. It would be compiler error. The compiler does not consider the return type while differentiating the overloaded method.

What is method overloading with type promotion?

method overloading with Type Promotion If a class has multiple methods having same name but different in parameters, it is known as Method Overloading. If we have to perform only one operation, having same name of the methods increases the readability of the program.

How do I implement function overloading?

A user can implement function overloading by defining two or more functions in a class sharing the same name.


1 Answers

Since including a module inserts it into the ancestor chain, you can just call super:

class User
  include Concerns::MyConcern

  def my_concern_magic(arg0)
    arg1 = [1, 2, 3]
    super(arg0, arg1)
  end
end
like image 162
Andrew Marshall Avatar answered Oct 19 '22 02:10

Andrew Marshall