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

M

Marc Heiler

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.
 
T

Tim Hunter

Marc said:
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.

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.
 
M

matt neuburg

Marc Heiler said:
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.

fetch is more general; if you want it to behave like []() in this case,
say array.fetch(666,nil). m.
 
R

Robert Klemme

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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,477
Latest member
ColumbusMa

Latest Threads

Top