Lazy initialization in Ruby using lambda
05 May 2010
If you want to use lazy initialization in Ruby the
||= trick is an easy and readable way to do it.
Rather than bury the actual “get fruit” logic inside of another function though, I’ve found it handy to use a Proc if that logic doesn’t need to be re-used elsewhere.