0
class Foo
  def self.bar()
    puts("bar")
  end
  private_class_method :bar

  def self.foo()
    self.bar()
  end
end

Given the above example, why does:

 > Foo.foo()
NoMethodError: private method `bar' called for Foo:Class

instead of:

 > Foo.foo()
=> bar

I'm trying to create a class/namespce composed of "helper" methods Thing.helper(...), etc... I would prefer to have the internal methods scoped privately so someone at least has to go out of their way to call them directly. Is there a way to achieve what I'm looking for?

Java equivalent:

class Foo {
  private static void bar() {
    System.out.println("bar");
  }
  public static void foo() {
    bar();
  }
}

1 Answers1

2

It's because you try to call your private method with explicit receiver, self. Try simply

def self.foo
  bar
end
Marek Lipka
  • 50,622
  • 7
  • 87
  • 91