【发布时间】:2010-09-20 11:32:52
【问题描述】:
这是一个简单的风格问题。在 Ruby 代码中声明访问控制的首选方式是什么?
示例 A:
#!/usr/bin/env ruby
class MyClass
def method1 # this is public by default
#...
end
protected # subsequent methods will be protected
def method2
#...
end
private # subsequent methods will be private
def method3
#...
end
public # subsequent methods will be public
def method4
#...
end
end
或示例 B:
#!/usr/bin/env ruby
class MyClass
def method1
#...
end
def method2
#...
end
def method3
#...
end
def method4
#...
end
public :method1, :method4
protected :method2
private :method3
end
在语法上,我喜欢示例 B。A 在 protected/private 方法之后声明的 public 方法之间引入了歧义,尽管我看不出为什么你不应该在将 method1 指定为 @ 之后直接调用它987654327@.
然而,这不是我喜欢的。对此行业定义的规范是什么?
【问题讨论】:
标签: ruby access-specifier