0

I have defined a method in module A. I want to call the same method from the moduleB

module A
  included do
    def self.some_func
    end
  end
end

module B
  some_func # It raise error (NoMethodError: undefined method). How solve this?
end

module C
  include A
  include B
end

This doesn't work. Is it possible to call a function that is defined by one module in another module?

Nandhini
  • 645
  • 7
  • 21
임근영
  • 193
  • 1
  • 3
  • 14
  • Did you try `extend A` in module B ? check this https://stackoverflow.com/questions/18973906/calling-module-method-into-another-module-in-ruby – Nandhini Apr 15 '19 at 01:40

1 Answers1

2

That module A should be raising an ArgumentError unless it also has an extends ActiveSupport::Concern at the top. Without ActiveSupport::Concern, you'd be calling the Module#included instance method here:

included do
  ...
end

but that requires an argument.

If you say this:

module A
  extend ActiveSupport::Concern
  included do
    def self.some_func
    end
  end
end

then you'll get the included you're trying to use and the module A that you're expecting.

Also, module B doesn't include A so it has nowhere to get some_func from so this:

module B
  some_func
end

will give you a NoMethodError. If you include A:

module B
  include A
  some_func
end

then it will work.

mu is too short
  • 426,620
  • 70
  • 833
  • 800