我有嵌套的方法调用。在嵌套方法调用期间,会在某个时刻创建一个值dataForLastStep,并原封不动地传递该值,直到最后一个方法才使用该值。这些方法如下所示:
(dataForLastStep是从methodOne中的startingData中计算出来的)
def methodOne(startingData)
#...Doing some stuff with startingData to end up with data
methodTwo(data, dataForLastStep)
end
def methodTwo(data, dataForLastStep)
#...Doing some stuff with data to make dataStep3
methodThree(dataStep3, dataForLastStep)
end
def methodThree(data, dataForLastStep)
#...Doing some stuff with data to also dataForLastStep
#All done
end发布于 2018-09-06 16:07:36
您尚未明确dataForLastStep的创建位置。我假设它是从methodOne中的startingData计算出来的。
一种方法是将dataForLastStep保留为实例变量。然后,您可以稍后远程引用它,只要它是从同一实例中调用的。
def methodOne(startingData)
data = some_stuff(startingData)
@dataForLastStep = some_other_stuff(startingData)
methodTwo(data)
end
def methodTwo(data)
dataStep3 = still_some_other_stuff(data)
methodThree(dataStep3)
end
def methodThree(data)
another_stuff(data, @dataForLastStep)
endhttps://stackoverflow.com/questions/52198762
复制相似问题