Skip to content

Use #fetch for arrays? #781

@andyw8

Description

@andyw8

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions