gpt4 book ai didi

ruby - 优化范围如何工作?

转载 作者:数据小太阳 更新时间:2023-10-29 07:59:23 25 4
gpt4 key购买 nike

这本质上是 Ruby Metaprogramming 2 的一个片段。在本节中,他们掩盖了这个例子,但没有真正的解释。

module MyRefinement
refine MyClass do
def my_method
"refined"
end
end
end

class MyClass
def my_method
"original"
end

def another_method
my_method
end
end

using MyRefinement

obj = MyClass.new

puts obj.my_method #=> "refined"
puts obj.another_method #=> "original"

当您从另一个方法调用 my_method 时,为什么不应用优化?

最佳答案

它避免了“有漏洞的”细化,例如,细化专门应用于您细化的方法。

http://yehudakatz.com/2010/11/30/ruby-2-0-refinements-in-practice/

在底部附近解释了此功能;简而言之:

[the] refinement should not leak [...]. If it did, it would mean that any call into any method could leak a refinement into that method, which is the opposite of the purpose of the feature.

关于ruby - 优化范围如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30466560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com