Forum: Ruby initialize

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.
9d4ec8946f933a18a1d15b094cc3c425?d=identicon&s=25 Jonathan Leighton (Guest)
on 2006-01-01 14:58
(Received via mailing list)
Should initialize() be public, private or protected, and why?

Thanks
956f185be9eac1760a2a54e287c4c844?d=identicon&s=25 ts (Guest)
on 2006-01-01 15:07
(Received via mailing list)
>>>>> "J" == Jonathan Leighton <lists@turnipspatch.com> writes:

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%



Guy Decoux
9d4ec8946f933a18a1d15b094cc3c425?d=identicon&s=25 Jonathan Leighton (Guest)
on 2006-01-01 16:51
(Received via mailing list)
On Sun, 2006-01-01 at 23:06 +0900, ts wrote:
> end
>
> p A.private_instance_methods(false)
> p A.protected_instance_methods(false)
> moulon%
>
> moulon% ./b.rb
> ["initialize"]
> []
> moulon%

Thanks for the reply. Forgive me, but I don't understand what the point
is in your example? Or why initialize is returned by
private_instance_methods() but not protected_instance_methods()? Could
you elaborate a bit please?

Thanks
956f185be9eac1760a2a54e287c4c844?d=identicon&s=25 ts (Guest)
on 2006-01-01 16:54
(Received via mailing list)
>>>>> "J" == Jonathan Leighton <lists@turnipspatch.com> writes:

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.


Guy Decoux
956f185be9eac1760a2a54e287c4c844?d=identicon&s=25 ts (Guest)
on 2006-01-01 17:00
(Received via mailing list)
>>>>> "t" == ts  <decoux@moulon.inra.fr> writes:

t>  #initialize (like #initialize_copy) is *always* a private method.
Even if
t>   you try to define it as public or protected, ruby will make it a
private
t>   method.

 Well, there is an exception when you redefine its state after the
creation

moulon% cat b.rb
#!/usr/bin/ruby
class A
   def initialize
   end
   protected :initialize
end

A.new
moulon%

moulon% ./b.rb
./b.rb:8:in `new': protected method `initialize' called for
#<A:0xb7d64e94> (NoMethodError)
	from ./b.rb:8
moulon%

Guy Decoux
9d4ec8946f933a18a1d15b094cc3c425?d=identicon&s=25 Jonathan Leighton (Guest)
on 2006-01-01 17:28
(Received via mailing list)
On Mon, 2006-01-02 at 00:53 +0900, ts wrote:
> >>>>> "J" == Jonathan Leighton <lists@turnipspatch.com> writes:
>
> 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.

Ah, okay, thanks.
This topic is locked and can not be replied to.