J
Jonathan Leighton
Should initialize() be public, private or protected, and why?
Thanks
Thanks
J> Should initialize() be public, private or protected, and why?
moulon% cat b.rb
#!/usr/bin/ruby
class A
protected
def initialize
end
end
p A.private_instance_methods(false)
p A.protected_instance_methods(false)
moulon%
moulon% ./b.rb
["initialize"]
[]
moulon%
J> Thanks for the reply. Forgive me, but I don't understand what the point
J> is in your example? Or why initialize is returned by
J> private_instance_methods() but not protected_instance_methods()? Could
J> you elaborate a bit please?
#initialize (like #initialize_copy) is *always* a private method. Even if
you try to define it as public or protected, ruby will make it a private
method.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.