documentation: "By default, all methods in Ruby classes are public - accessible by anyone. There are, nonetheless, only two exceptions for this rule: the global methods defined under the Object class, and the initialize method for any class. Both of them are implicitly private.">> class Object >> def something >> puts "something" >> end >> end=> nil>> class XYZ >> end=> nil>> xyz = XYZ.new=> #<XYZ:0x10d067580>>> xyz.somethingsomething So then why was I able to access something? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
1) Post the definition of private. 2) Execute this program: puts ''hello'' 3) What conclusions do you draw from that? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
John Merlino
2012-Oct-03 05:08 UTC
Re: global methods under object supposedly unaccessible
A private method is internal to the implementation of a class, and it can only be called by other instance methods of the class (or its subclasses). Private methods are implicitly invoked on self, and may not be explicitly invoked on an object. If m is a private method, then you must ibnvoke it in functional style as m. But look at this: 1.9.3p0 :032 > class Object 1.9.3p0 :033?> def apple2 1.9.3p0 :034?> puts ''apple2'' 1.9.3p0 :035?> end 1.9.3p0 :036?> end => nil 1.9.3p0 :037 > self.apple2 apple2 => nil Or this: 1.9.3p0 :038 > def apple2 1.9.3p0 :039?> puts ''apple2'' 1.9.3p0 :040?> end => nil 1.9.3p0 :041 > self.apple2 apple2 => nil I invoked apple2 on self (an explicit receiver). However, according to documentation, defining apple2 in Object should have made it private. Only this would work: 1.9.3p0 :025 > class Object 1.9.3p0 :026?> private 1.9.3p0 :027?> def apple1 1.9.3p0 :028?> puts ''apple1'' 1.9.3p0 :029?> end 1.9.3p0 :030?> end => nil 1.9.3p0 :031 > self.apple1 NoMethodError: private method `apple1'' called for main:Object However, in the above example, I was forced to use the private method. The documentation says "any global methods declared outside of a class definition - those methods are defined as private instance methods of Object." In my first examples, I defined a method outside of class definition. But was still able to invoke it on self (self being Object in that case). On Sep 23, 7:16 pm, 7stud -- <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 1) Post the definition ofprivate. > > 2) Execute this program: > > puts ''hello'' > > 3) What conclusions do you draw from that? > > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Frederick Cheung
2012-Oct-03 09:30 UTC
Re: global methods under object supposedly unaccessible
On Wednesday, October 3, 2012 6:08:31 AM UTC+1, John Merlino wrote:> > > However, in the above example, I was forced to use the private method. > The documentation says "any global methods declared outside of a class > definition - those methods are defined as private instance methods of > Object." > In my first examples, I defined a method outside of class definition. > But was still able to invoke it on self (self being Object in that > case). > > This is one of those cases where irb behaves slightly differently to a''normal'' ruby script. If you copy that code into a standalone file and run that then the call to self.apple2 will raise an exception Fred> On Sep 23, 7:16 pm, 7stud -- <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > > 1) Post the definition ofprivate. > > > > 2) Execute this program: > > > > puts ''hello'' > > > > 3) What conclusions do you draw from that? > > > > -- > > Posted viahttp://www.ruby-forum.com/. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/xeLeTIDfq1cJ. For more options, visit https://groups.google.com/groups/opt_out.