Closed sheerun closed 11 years ago
Do you propose using fetch in absolutely every case?
Same as with any other method call, hash#[] is no different, this is ruby, you can get nil enywhere, using fetch gives nothing really.
{:a => nil}.fetch(:a).foo # => still NME
@teamon Consider another use case:
{:a => nil}.fetch(:b).foo # => IndexError: key not found
{:a => nil}[:b].foo # => NoMethodError
I think it makes sense in some situations as it can produce better backtrace. I would put it in guidelines with annotation "Consider using fetch instead of [](key)
", not as a default.
@jandudulski Of course not...
"Consider" sounds good :)
:+1: for "Consider..."
So the error is visible immediately and not "undefined method ... for nil:NilClass"