Forum: Ruby Can someone explain the difference: Array[] and Array.fetch

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
4828d528e2e46f7c8160c336eb332836?d=identicon&s=25 Robert Heiler (shevegen)
on 2009-06-06 03:03
array = ["a","b","c"]        # => ["a", "b", "c"]
array[1]                     # => "b"
array.fetch 1                # => "b"


# ================================================== #
# So far, so fine.
# ================================================== #


array[666]                   # => nil
array.fetch 666

IndexError: index 666 out of array
        from (irb):5:in `fetch'
        from (irb):5


This may seem like a trival difference between the two, but
I am wondering in general why they behave differently.
Or in other words, why array[] does not return an error
whereas .fetch specifically does.
3afd3e5e05dc9310c89aa5762cc8dd1d?d=identicon&s=25 Tim Hunter (Guest)
on 2009-06-06 03:40
(Received via mailing list)
Marc Heiler wrote:
> array[666]                   # => nil
> whereas .fetch specifically does.
You might say that's the purpose of fetch, to raise an exception for
out-of-bounds indexes when [] just returns nil. Pick the method you want
depending on the error handling you want.
Fbb4d027695dfdf76bf448b15d7e306a?d=identicon&s=25 matt neuburg (Guest)
on 2009-06-06 03:51
(Received via mailing list)
Marc Heiler <shevegen@linuxmail.org> wrote:

> array[666]                   # => nil
> whereas .fetch specifically does.
fetch is more general; if you want it to behave like []() in this case,
say array.fetch(666,nil). m.
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-06-06 13:31
(Received via mailing list)
On 06.06.2009 03:03, Marc Heiler wrote:
> array[666]                   # => nil
> whereas .fetch specifically does.
I am not the author of that class so I can only speculate.  The reason
is to have a proper tool for different situations.  Often you do not
care whether the array index does exist.  For example, with [] you can
do

x = []
x[10] = 123

Without having to worry about array dimensions.

If on the other hand you want to see an error if an index out of the
current range is used or want to provide a default value you can use
#fetch.  Btw, these methods are defined with similar semantics for Hash.

Kind regards

  robert
4828d528e2e46f7c8160c336eb332836?d=identicon&s=25 Robert Heiler (shevegen)
on 2009-06-06 19:31
Thanks to all for the explanation!
Ee2809522b2e56d0d6b656486bc5e0db?d=identicon&s=25 Markus Fischer (Guest)
on 2009-06-06 20:01
(Received via mailing list)
Hi,

I too learned a lot from that thread about this. Since I'm still new to
ruby I've to use the docs [1] nearly daily. I wish this kind of "hint"
would be added to both methods ...

- Markus

[1] e.g. http://www.ruby-doc.org/core/classes/Array.html
This topic is locked and can not be replied to.