F
Fasun Lau
[Note: parts of this message were removed to make it a legal post.]
Hi, all
I'm reading "Programming Ruby", and have some questions about "protected"
and "private" of class.
In "Programming Ruby" :
Protected methods can be invoked only by objects of the defining class and
its subclasses. Access is kept within the family.
Private methods cannot be called with an explicit receiver. Because you
cannot specify an object when using them, private methods can be called only
in the defining class and by direct descendents within that same object.
The difference between "protected" and "private" is
fairly subtle, *and is different in Ruby than in most common OO languages*. If
a method is protected, it may be called by any instance of the defining
class or its subclasses. If a method is private, it may be called only
within the context of the calling object---it is never possible to access
another object's private methods directly, even if the object is of the same
class as the caller.
I was tried it with the follow code:
C:\Users\v-fsliu>ruby -v
ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-mswin32]
C:\Users\v-fsliu>irb
irb(main):001:0> class Demo
irb(main):002:1> def call1
irb(main):003:2> method1 + "Object ID: " + self.object_id.to_s
irb(main):004:2> end
irb(main):005:1>
irb(main):006:1* def call2
irb(main):007:2> method2 + "Object ID: " + self.object_id.to_s
irb(main):008:2> end
irb(main):009:1>
irb(main):010:1* protected
irb(main):011:1> def method1
irb(main):012:2> "Hello, Ruby! "
irb(main):013:2> end
irb(main):014:1>
irb(main):015:1* private
irb(main):016:1> def method2
irb(main):017:2> "Hi, Ruby! "
irb(main):018:2> end
irb(main):019:1> end
=> nil
irb(main):020:0>
irb(main):021:0* class NewDemo < Demo
irb(main):022:1> def newcall
irb(main):023:2> newmethod + "Object ID: " + self.object_id.to_s
irb(main):024:2> end
irb(main):025:1>
irb(main):026:1* protected
irb(main):027:1> def newmethod
irb(main):028:2> "Hello, new demo! "
irb(main):029:2> end
irb(main):030:1> end
=> nil
irb(main):031:0> demo = Demo.new
=> #<Demo:0x3ae9284>
irb(main):032:0> demo.call1
=> "Hello, Ruby! Object ID: 30886210"
irb(main):033:0> demo.call2
=> "Hi, Ruby! Object ID: 30886210"
irb(main):034:0> demo.method1
NoMethodError: protected method `method1' called for #<Demo:0x3ae9284>
from (irb):34
from :0
irb(main):035:0> newDemo = NewDemo.new
=> #<NewDemo:0x3ae2a24>
irb(main):036:0> newDemo.newcall
=> "Hello, new demo! Object ID: 30872850"
irb(main):037:0> newDemo.newmethod
NoMethodError: protected method `newmethod' called for #<NewDemo:0x3ae2a24>
from (irb):37
from :0
irb(main):038:0>
My questions are:
1. It seems that protected methodcalled by any instance of the defining
class or its subclasses.
2. what's the different in Ruby than in most common OO languages.
Thanks and best regards!
Hi, all
I'm reading "Programming Ruby", and have some questions about "protected"
and "private" of class.
In "Programming Ruby" :
Protected methods can be invoked only by objects of the defining class and
its subclasses. Access is kept within the family.
Private methods cannot be called with an explicit receiver. Because you
cannot specify an object when using them, private methods can be called only
in the defining class and by direct descendents within that same object.
The difference between "protected" and "private" is
fairly subtle, *and is different in Ruby than in most common OO languages*. If
a method is protected, it may be called by any instance of the defining
class or its subclasses. If a method is private, it may be called only
within the context of the calling object---it is never possible to access
another object's private methods directly, even if the object is of the same
class as the caller.
I was tried it with the follow code:
C:\Users\v-fsliu>ruby -v
ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-mswin32]
C:\Users\v-fsliu>irb
irb(main):001:0> class Demo
irb(main):002:1> def call1
irb(main):003:2> method1 + "Object ID: " + self.object_id.to_s
irb(main):004:2> end
irb(main):005:1>
irb(main):006:1* def call2
irb(main):007:2> method2 + "Object ID: " + self.object_id.to_s
irb(main):008:2> end
irb(main):009:1>
irb(main):010:1* protected
irb(main):011:1> def method1
irb(main):012:2> "Hello, Ruby! "
irb(main):013:2> end
irb(main):014:1>
irb(main):015:1* private
irb(main):016:1> def method2
irb(main):017:2> "Hi, Ruby! "
irb(main):018:2> end
irb(main):019:1> end
=> nil
irb(main):020:0>
irb(main):021:0* class NewDemo < Demo
irb(main):022:1> def newcall
irb(main):023:2> newmethod + "Object ID: " + self.object_id.to_s
irb(main):024:2> end
irb(main):025:1>
irb(main):026:1* protected
irb(main):027:1> def newmethod
irb(main):028:2> "Hello, new demo! "
irb(main):029:2> end
irb(main):030:1> end
=> nil
irb(main):031:0> demo = Demo.new
=> #<Demo:0x3ae9284>
irb(main):032:0> demo.call1
=> "Hello, Ruby! Object ID: 30886210"
irb(main):033:0> demo.call2
=> "Hi, Ruby! Object ID: 30886210"
irb(main):034:0> demo.method1
NoMethodError: protected method `method1' called for #<Demo:0x3ae9284>
from (irb):34
from :0
irb(main):035:0> newDemo = NewDemo.new
=> #<NewDemo:0x3ae2a24>
irb(main):036:0> newDemo.newcall
=> "Hello, new demo! Object ID: 30872850"
irb(main):037:0> newDemo.newmethod
NoMethodError: protected method `newmethod' called for #<NewDemo:0x3ae2a24>
from (irb):37
from :0
irb(main):038:0>
My questions are:
1. It seems that protected methodcalled by any instance of the defining
class or its subclasses.
2. what's the different in Ruby than in most common OO languages.
Thanks and best regards!