You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The guide advises using fetch for hashes. I discovered recently that Array also supports fetch, so that attempting to retrieve a non-existing element raises an IndexError rather than returning nil:
> x = ['a','b']
=> ["a", "b"]
> x[4]
=> nil
> x.fetch(4)
IndexError: index 4 outside of array bounds: -2...2
There may be situations where the nil behaviour is convenient, but I suspect that the majority of the time, it would be preferable to raise an error to avoid nils being unintentionally passed around.
Any thoughts?
The text was updated successfully, but these errors were encountered:
The guide advises using
fetch
for hashes. I discovered recently that Array also supportsfetch
, so that attempting to retrieve a non-existing element raises anIndexError
rather than returningnil
:There may be situations where the nil behaviour is convenient, but I suspect that the majority of the time, it would be preferable to raise an error to avoid nils being unintentionally passed around.
Any thoughts?
The text was updated successfully, but these errors were encountered: