From:
http://weblog.jamisbuck.org/2007/2/23/method-visibility-in-ruby
A common point of confusion to even experienced Ruby programmers is the visibility of public, protected, and private methods in Ruby classes. This largely stems from the fact that the behavior of those keywords in Ruby is different from what you might have learned from Java and C.
To demonstrate these differences, let’s set up a little script:
class Foo
def a; end
# call 'a' with explicit 'self' as receiver
def b; self.a; end
# call 'a' with implicit 'self' as receiver
def c; a; end
end
def safe_send(receiver, method, message)
# can't use 'send' because it bypasses visibility rules
eval "receiver.#{method}"
rescue => e
puts "#{message}: #{e}"
else
puts "#{message}: succeeded"
end
visibility = ARGV.shift || "public"
Foo.send(visibility, :a)
foo = Foo.new
safe_send(foo, :a, "explicit receiver ")
safe_send(foo, :b, "explicit 'self' receiver")
safe_send(foo, :c, "implicit 'self' receiver")
Basically, the script just creates a class “Foo” with three methods: a, which we’ll invoke directly with an explicit, non-self receiver; b, which invokes a with self as receiver, and c, which invokes a with an implicit receiver of self. We’ll use the safe_send method to call each of those methods and log the result.
So, first: the public keyword. In Ruby, public means that the method may be invoked just about any way you please; in technical terms, the receiver of the message may be either explicit (“foo.bar”), self (“self.bar”) or implicit (“bar”).
引用
$ ruby demo.rb public
explicit receiver : succeeded
explicit 'self' receiver: succeeded
implicit 'self' receiver: succeeded
The protected keyword puts a straitjacket around the method. Any method declared protected may only be called if the receiver is self, explicitly or implicitly. (Update: protected methods may actually be called any time the receiver is of the same class as ‘self’...and an explicit self as receiver is just a specific case of that. Modifying the script to demonstrate this condition is left as an exercise for the reader.)
引用
$ ruby demo.rb protected
explicit receiver : protected method `a' called for #<Foo:0x3fc18>
explicit 'self' receiver: succeeded
implicit 'self' receiver: succeeded
Lastly, the private keyword is the tightest setting of all. A private method cannot be called with an explicit receiver at all, even if that receiver is “self”.
引用
$ ruby demo.rb private
explicit receiver : private method `a' called for #<Foo:0x3fc18>
explicit 'self' receiver: private method `a' called for #<Foo:0x3fc18>
implicit 'self' receiver: succeeded
Note that, unlike languages such as Java, inheritance plays absolutely no part in determining method visibility in Ruby. Subclasses can access both protected and private methods of the superclass without trouble, so long as they abide by the rules laid out above.
The difference between protected and private is very subtle, as you can see, which explains why protected is rarely used by most Rubyists. If it is used at all, it is generally as a convention, to document methods that are internal to the class, but which lie closer to the public interface than others. In Rails, for instance, you might declare your controller filter methods and model validation methods as “protected” (because the framework will call those methods) and reserve the “private” designation for those methods that are only ever called from within your own model or controller code.
分享到:
相关推荐
2.2 Computing Visibility of a Point in Simple Polygons 16 2.2.1 Non-Winding Polygon: O(n) Algorithm 16 2.2.2 Removing Winding: O(n) Algorithm 23 2.3 Computing Visibility of a Point in Polygons with ...
Modeling Mutual Visibility Relationship in Pedestrian Detection Modeling Mutual Visibility Relationship in Pedestrian Detection Modeling Mutual Visibility Relationship in Pedestrian Detection
该方法为在有雾天气下去雾的一种方法,有很高的参考价值
Managing visibility in Flutter – Flutter – Medium
「DDoS」Gaining Endpoint Log Visibility in ICS Environments - 法律法规 漏洞挖掘 安全防御 工控安全 安全众测 DDoS
WPF的bool2Visibility转换器使用
地形渲染加速,可视剔除算法。适用VR、游戏研发等领域
VISIBILITY属性的详解包括VISIBLE、INVISIBLE及GONE区别
IEEE Transactions On Instrument and Measurement
Android开发中,大部分控件都有visibility这个属性,其属性有3个分别为“visible ”、“invisible”、“gone”。主要用来设置控制控件的显示和隐藏。
display与visibility的区别
前端项目-visibility.js,Wrapper for the Page Visibility API
A novel polarimetric dehazing method is proposed based on three linear polarization images (0°, 45°, and 90°). The polarization orientation angle of the light scattered by the haze particles is ...
在线社交网络中的社交可见性服务定价模型与算法_Pricing Social Visibility Service in Online Social Networks Modeling and Algorithms.pdf
使用CSS3 backface-visibility属性制作3D翻转动画效果.zip 使用CSS3 backface-visibility属性制作3D翻转动画效果.zip 使用CSS3 backface-visibility属性制作3D翻转动画效果.zip
通过绑定修改DataGridColumns的Visibility
virtual netflow visibility,my test
CSS隐藏元素 display visibility opacity的区别 display:none和visibility:hidden的区别 对比总结: height:0和overflow:hidden的组合
Flutter Offstage、Visibility隐藏/可见。