Is there a convention for memoization in a method call?

前端 未结 6 1534
清酒与你
清酒与你 2021-02-19 04:14

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
         


        
6条回答
  •  星月不相逢
    2021-02-19 04:53

    I would do it like this:

    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.

提交回复
热议问题