我ruby 1.8.7,为什么我可以在main中使用require,但不能使用self.require?
require('date') # ok
self.require('date')
NoMethodError: private method `require' called for main:Object
from (irb):22
from /usr/lib/ruby/1.8/date.rb:437众所周知,main是对象类: irb( main ):045:0>自身=> main
irb(main):043:0> self.class
=> Object但我发现它有内核混合:
irb(main):042:0> self.class.included_modules
=> [Kernel]此外,我发现require是self的私有方法:
irb(main):037:0> self.private_methods
=> [... "require", ...]同样,我不能使用self.attr_accessor:
irb(main):051:0> class X
irb(main):052:1> self.attr_accessor(:ssss)
irb(main):053:1> end
NoMethodError: private method `attr_accessor' called for X:Class
from (irb):52
from /usr/lib/ruby/1.8/date.rb:437它是如何发生的?有人能澄清这个问题吗?
发布于 2012-04-25 20:39:26
看看下面这个简单的例子:
class Person
def initialize(age)
@age = age
end
def compare_to(other)
# we're calling a protected method on the other instance of the current class
age <=> other.age
end
# it will not work if we use 'private' here
protected
def age
@age
end
end在ruby中,我们有隐式和显式的方法接收器,请检查下面的代码片段:
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基本上,在ruby中,如果一个方法是private,那么它只能在implicit接收器上调用(没有self关键字)。在您的示例中,require是一个定义在Kernel模块中的private方法,它只能在implicit主题上调用。
发布于 2012-04-25 20:52:00
require是一个私有方法。所以你不能把它叫做
Object.require 'date'但是你可以用ruby的eval/send方法来调用它:
Object.send(:require, 'date')
# or
self.send(:require', 'date')实际上非常类似于
require 'date'例如,pry console会将其解释为
instance_exec do
require 'date'
end我相信Ruby解释器也会做同样的事情。它会将任何顶级命令作为instance_exec块传递给Object,后者可以调用任何私有方法。
发布于 2012-04-25 21:28:38
只能使用隐式接收器调用私有方法。这意味着require可以工作,但self.require不能。
受保护的方法可以在self上调用,公共方法可以在任何对象上显式调用。
这些是唯一的限制。是的,您可以在子类中使用私有方法,并且send将绕过所有的访问控制。
https://stackoverflow.com/questions/10314367
复制相似问题