Class methods and protected instance methods

I would have thought that a class method context would be able to call
protected methods on an instance of that class. That does not seem to be
the case:

class Foo
  def self.create_with_magic
    newfoo = new()
    newfoo.init_magic
    return newfoo
  end

  def initialize
    # no magic here
  end

  protected

  def init_magic
    # something magical
  end
end

foo = Foo.create_with_magic #this will raise an exception

Is this just an oversight? A bug? Is there a rationale for it that I'm not
seeing? I'm not looking for a workaround; I've already found a better way
of doing what brought this to my attention. I am just looking for an
explanation.

--Greg

This is what is expected. Ruby is not C++/Java and its object model is
quite different. In particular as Class objects are first class
objects, they do not bestow any special access rights to other
objects, including instances of themselves.

pth

···

On 5/1/06, Gregory Seidman <gsslist+ruby@anthropohedron.net> wrote:

I would have thought that a class method context would be able to call
protected methods on an instance of that class. That does not seem to be
the case:

class Foo
  def self.create_with_magic
    newfoo = new()
    newfoo.init_magic
    return newfoo
  end

  def initialize
    # no magic here
  end

  protected

  def init_magic
    # something magical
  end
end

foo = Foo.create_with_magic #this will raise an exception

Is this just an oversight? A bug? Is there a rationale for it that I'm not
seeing? I'm not looking for a workaround; I've already found a better way
of doing what brought this to my attention. I am just looking for an
explanation.

--Greg