这是一个简单的风格问题。在 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 在/方法public
之后声明的方法之间引入了歧义,尽管我认为没有理由不应该在将其指定为之后调用。protected
private
method1
public
然而,这不是我喜欢的。行业定义的规范是什么?