在方法调用中有记忆的约定吗? [英] Is there a convention for memoization in a method call?
本文介绍了在方法调用中有记忆的约定吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想避免重新评估方法调用中的值。直到现在,我是这样做的:
I want to avoid reevaluation of a value in method call. Untill now, I was doing this:
def some_method
@some_method ||= begin
# lot's of code
end
end
但它最终非常难看。在一些代码中,我看到类似下面的东西:
But it ends up quite ugly. In some code, I saw something like the following:
def some_method
@some_method ||= some_method!
end
private
def some_method!
# lot's of code
end
我不喜欢bang !
),所以我想出了这个:
I don't like the bang (!
) at the end, so I came up with this:
def some_method
@some_method ||= _some_method
end
private
def _some_method
# lot's of code
end
- 前面加上下划线好的约定? $ b
- 是否有一些约定来记忆多行方法?
- Is prepending with an underscore a good convention?
- Is there some other convention for memoized/non-memoized pairs of methods?
- Is there some convention to memoize multi-line methods?
推荐答案
我会这样做:
def filesize
@filesize ||= calculate_filesize
end
private
def calculate_filesize
# ...
end
所以我只是命名方法不同,感觉。
So I'd just name the method differently, as I think it makes more sense.
这篇关于在方法调用中有记忆的约定吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文