2012-06-08 19 views
1
class Curious 
    def calculation 
    def calculation 
     @retrieved_value 
    end 
    @retrieved_value = #some kind of intensive process 
    end 
end 

这样做,外部方法将在第一次运行,内部方法将提供值的后续时间。在一个非嵌套方法中做这件事的优点或缺点是什么@retrieved_value ||= #some kind of intensive processRuby范围化的方法值技术

+0

“嵌套方法”可能是一个坏名词,因为该方法在“封闭”方法的范围之外是可用的。 –

回答

2

您正在为Curious类重新定义calculation方法。这将影响其他实例:

a = Curious.new 
a.calculation # calls "outer" method, this sets @retrieved_value 
a.calculation # calls "inner" method 

b = Curious.new 
b.calculation # calls "inner" method, @retrieved_value not set 
1

国际海事组织几乎没有优势,缺点是乍一看有点不透明。

根据密集过程的性质,可能会有一些范围优势。