元编程Ruby第3章有一个任务来编写C#的using
statement的Ruby等价物。我开始了:Ruby的使用方法是什么?
class Resource
def dispose
@disposed = true
end
def disposed?
@disposed
end
end
def using(r)
puts "Not implemented."
end
r = Resource.new
using(r)
我还没有执行using
呢。然而,当我运行这段代码,我得到
in `using': wrong argument type Resource (expected Module) (TypeError)
而且,如果我喜欢写东西using(Kernel)
,using(Enumerable)
等,在程序完成没有错误。据我所知,在Ruby中没有using
方法或关键字,但我也在pry和irb中获得相同的行为。发生什么事?
我得到“未实现。”当我运行上面的代码时,为我工作的方式应该是1.9.3 – hirolau
您运行的是什么ruby版本?在后面的MRI版本中不应该有一种叫'使用'的方法 – phoet
你可以看到[here](http://www.ruby-doc.org/core-2.0.0/doc/syntax/refinements_rdoc.html),当我们需要使用'using'方法.'''将模块名称作为参数,而不是对象。你得到了错误,当你通过'Resource'的实例时,'使用'用于激活细化。请参阅此博客['Refining Ruby'](http://blog.headius.com/2012/11/refining-ruby.html) –