Forum: Ruby class methods and protected instance methods

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bf6862e2a409078e13a3979c00bba1d6?d=identicon&s=25 Gregory Seidman (Guest)
on 2006-05-03 18:57
(Received via mailing list)
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
2c51fec8183a5d21c4e11b430beabb47?d=identicon&s=25 Patrick Hurley (Guest)
on 2006-05-04 15:49
(Received via mailing list)
On 5/1/06, Gregory Seidman <gsslist+ruby@anthropohedron.net> wrote:
>
>
>
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
This topic is locked and can not be replied to.