Reputation: 6415
class Curious
def calculation
def calculation
@retrieved_value
end
@retrieved_value = #some kind of intensive process
end
end
Doing the above, the outer method will be run the first time and the inner method will provide the value subsequent times. What's the advantage or disadvantage of doing that over a single non-nested method that just does @retrieved_value ||= #some kind of intensive process
?
Upvotes: 1
Views: 88
Reputation: 114178
You're redefining the calculation
method for the Curious
class. This will affect other instances:
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
Upvotes: 2
Reputation: 160191
IMO there's little advantage, with a disadvantage of being somewhat opaque at first glance.
It's possible there could be some scoping advantages depending on the nature of the intensive process.
Upvotes: 1