Tadej Mali
Tadej Mali

Reputation: 1143

Ruby module methods accessibility

I have the following module:

module Foo
  def f1 x
   puts "f1(#{x})"
  end
  def Foo.f2 x
   puts "f2(#{x})"
  end
end

When it is included into a class:

class Bar
    include Foo

    Foo.f2 "bar"   # This works
    f1 "bar"       # Missing method

    def b x
        f1 x       # This works too
    end
end

Why is the behavior of f1 different in the two cases?

How is the scope of module metods defined?

Can f1 be written in such a way, that it would work as in the case f1 "bar"? Like for example the task in Rakefile?

Upvotes: 0

Views: 187

Answers (1)

Dave Newton
Dave Newton

Reputation: 160191

If you want to add class methods to Bar you'd want to use included; see Yehuda's post on the subject. It depends (somewhat) on what you're actually trying to do.

Upvotes: 1

Related Questions