2
class Foo
  include Bar
  include Baz
end

module Bar
  def do_something
  end
end

module Baz
  do_something
end

Baz module does not have access to Bar. Is there a way for it to call a method in Bar?

One approach is to extend Baz with Bar, but I want to include them all in Foo instead.

1
  • Because do_something is only called from Baz. Commented Sep 2, 2016 at 19:24

1 Answer 1

2

If for whatever reason you don't want to extend Baz with Bar, you can create an object that extends Bar inside Baz:

module Bar
  def do_something
    puts 42
  end
end

module Baz
  Object.new.extend(Bar).do_something # prints 42
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.