Thu Dec 1
#
The example assumes that the option is always set (either by a default or otherwise).
Tip Ensure an option hash value is a collection, but flatten it (without flatten)
Occasionally one sees:
def foo(options = {})
[options[:bar]].flatten.map do |arg|
- …
end
end
If options[:bar] is a single value you want it to be treated like a collection. If it’s a collection then you want to flatten it so the surrounding [] doesn’t create a collection within a collection.
Spat rather than flatten:
def foo(options = {})
[*options[:bar]].map do |arg|
- …
end
end
The example assumes that the option is always set (either by a default or otherwise).
(Thank you to Ryan Davis for making me realize that there was something wrong with my initial post)
#
“ You know what? I’m pretty happy working from a detailed functional spec. ”
Obie Fernandez
#
“ Elegance is not optional. ”
Richard O’Keefe