Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use #fetch for arrays? #781

Open
andyw8 opened this issue Nov 17, 2019 · 0 comments
Open

Use #fetch for arrays? #781

andyw8 opened this issue Nov 17, 2019 · 0 comments
Labels

Comments

@andyw8
Copy link
Contributor

andyw8 commented Nov 17, 2019

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant