Ruby 访问类方法时点(.)和双冒号(::)之间的差异
双冒号(::)允许从类或模块之外的任何位置访问类或模块内定义的常量、实例方法和类方法 看看这个例子:Ruby 访问类方法时点(.)和双冒号(::)之间的差异,ruby,Ruby,双冒号(::)允许从类或模块之外的任何位置访问类或模块内定义的常量、实例方法和类方法 看看这个例子: class Sample VAR_SAMPLE="what is the difference?" def self.show_var return VAR_SAMPLE end def method2 return VAR_SAMPLE end end puts Sample::show_var # => what is t
class Sample
VAR_SAMPLE="what is the difference?"
def self.show_var
return VAR_SAMPLE
end
def method2
return VAR_SAMPLE
end
end
puts Sample::show_var # => what is the difference?
puts Sample.show_var # => what is the difference?
puts Sample::new::method2 # => what is the difference?
puts Sample.new.method2 # => what is the difference?
那么,使用点(.)和双冒号(:)操作符访问类方法有什么区别呢?任何想法都很感激。您可以使用以下方法调用ruby方法 使用
点(.)
,双冒号(::)
,发送
方法&方法
方法
class Sample
VAR_SAMPLE="what is the difference?"
def self.show_var
return VAR_SAMPLE
end
def method2
return VAR_SAMPLE
end
end
puts Sample::show_var
puts Sample.show_var
puts Sample.send(:show_var)
puts Sample.method(:show_var).call
puts Sample::new::method2
puts Sample.new.method2
puts Sample.send(:new).send(:method2)
puts Sample.method(:new).call.method(:method2).call
# All the above will return `what is the difference?` only
现在考虑的是私有< /P>方法
class Sample
VAR_SAMPLE="what is the difference?"
private
def self.show_var
return VAR_SAMPLE
end
end
puts Sample::show_var # Will throw error private method `show_var' called for
puts Sample.show_var # Will throw error private method `show_var' called for
puts Sample.send(:show_var) # what is the difference?
puts Sample.method(:show_var).call # what is the difference?
注意:-除此之外,您还可以使用其他
元编程方法调用ruby方法。您可以使用以下方法调用ruby方法
使用点(.)
,双冒号(::)
,发送
方法&方法
方法
class Sample
VAR_SAMPLE="what is the difference?"
def self.show_var
return VAR_SAMPLE
end
def method2
return VAR_SAMPLE
end
end
puts Sample::show_var
puts Sample.show_var
puts Sample.send(:show_var)
puts Sample.method(:show_var).call
puts Sample::new::method2
puts Sample.new.method2
puts Sample.send(:new).send(:method2)
puts Sample.method(:new).call.method(:method2).call
# All the above will return `what is the difference?` only
现在考虑的是私有< /P>方法
class Sample
VAR_SAMPLE="what is the difference?"
private
def self.show_var
return VAR_SAMPLE
end
end
puts Sample::show_var # Will throw error private method `show_var' called for
puts Sample.show_var # Will throw error private method `show_var' called for
puts Sample.send(:show_var) # what is the difference?
puts Sample.method(:show_var).call # what is the difference?
注意:-除此之外,您还可以使用其他元编程方法调用ruby方法。双冒号:
名称空间操作符也可以用作消息发送操作符。换句话说,
foo.bar
也可以写成
foo::bar
除非不是
特别是,
始终是一个消息发送<代码>:
通常是一个名称空间查找,除非它不可能是。例如,这意味着您不能调用以大写字符开头的消息,除非您同时传递参数列表
foo=Class.new do
def BAR;:方法端
BAR=:常数
结束
foo.BAR#=>:方法
foo::BAR#=>:常数
foo::BAR()#=>:方法
::
也可以用于消息发送这一事实是历史上的一件奇事,除了“类工厂”(即返回类的方法)之外,在大多数样式指南中都被禁止。想象一下这样设置的web框架:
模块控制器
def self.R(路径)
新建(AbstractController)do
#处理路由到路径的一系列方法`
结束
结束
结束
类IndexController
在这种情况下,在一些样式指南中,可以编写Controller::R
,因为即使R
是一个方法,它也会返回一个类,因此它的行为类似于一个类
但这是某些DSL的特例,仅在某些样式指南中允许。大多数样式指南不允许对消息发送使用::
,因为它与
是冗余的,因为它已经有了另一个不同的含义(名称空间解析),并且在所有情况下,它的行为都不像
那么,使用点(.)和双冒号(:)操作符访问类方法有什么区别呢
一方面,您可以说,没有区别,因为当用作消息发送操作符时,它们都做完全相同的事情
另一方面,语法上存在差异,即foo::BAR
不是消息发送,而是完全不同的名称空间查找。从foo.BAR
,这是一个消息发送。双冒号:
名称空间操作符也可以用作消息发送操作符。换句话说,
foo.bar
也可以写成
foo::bar
除非不是
特别是,
始终是一个消息发送<代码>:
通常是一个名称空间查找,除非它不可能是。例如,这意味着您不能调用以大写字符开头的消息,除非您同时传递参数列表
foo=Class.new do
def BAR;:方法端
BAR=:常数
结束
foo.BAR#=>:方法
foo::BAR#=>:常数
foo::BAR()#=>:方法
::
也可以用于消息发送这一事实是历史上的一件奇事,除了“类工厂”(即返回类的方法)之外,在大多数样式指南中都被禁止。想象一下这样设置的web框架:
模块控制器
def self.R(路径)
新建(AbstractController)do
#处理路由到路径的一系列方法`
结束
结束
结束
类IndexController
在这种情况下,在一些样式指南中,可以编写Controller::R
,因为即使R
是一个方法,它也会返回一个类,因此它的行为类似于一个类
但这是某些DSL的特例,仅在某些样式指南中允许。大多数样式指南不允许对消息发送使用::
,因为它与
是冗余的,因为它已经有了另一个不同的含义(名称空间解析),并且在所有情况下,它的行为都不像
那么,使用点(.)和双冒号(:)操作符访问类方法有什么区别呢
一方面,您可以说,没有区别,因为当用作消息发送操作符时,它们都做完全相同的事情
另一方面,语法上存在差异,即
foo::BAR
不是消息发送,而是完全不同的名称空间查找。从发送消息的foo.BAR
。类方法不尊重private
,您可以调用Sample.show\u var
在上一个示例omg中,我不知道private在类方法中不起作用_T@slam但将其添加到本征类中,例如,类样本;VAR_SAMPLE=“有什么区别?”;类我猜,类方法不尊重private
,背后有一些原因,你可以调用示例。在你的上一个示例中,omg,我不知道private在m类中不起作用