You’ve been using Hash#fetch and are happy with it.
You also know that you can specify default return value in case key you are looking for is not present in hash.
1 2 3 4 5 | |
Line 3 and 5 return the same result so what’s the difference you may ask? Lets use a complex method for a default value
which takes a couple of seconds to process. We will simulate slowness with sleep. Here is a code with
some benchmarks:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |
See how expensive_method gets executed in argument - key present benchmark blocking everything for 3+ seconds
even know :test key is present. We want expensive_method to be run only when :test key is not present and that is
why you should use block when specifying default value. Block gets executed only when the key is not present as opposed to argument
version which always executes expensive_method no matter if the key is present or not. While this probably won’t bite most of
you, I still think it is good to know about this “gotcha”.
I learned about this while watching one of the RubyTapas episodes. RubyTapas are short screencasts related to Ruby by Avdi Grimm. If you’re not subscribed to this resource I highly encourage you to do so - knowledge bombs Avdi keeps dropping are really invaluable.