[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Marc Heiler

6/6/2009 1:04:00 AM

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.
--
Posted via http://www.ruby-....

5 Answers

Tim Hunter

6/6/2009 1:40:00 AM

0

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

--
MagickWand for Ruby - http://magickwand.ruby...

matt

6/6/2009 1:46:00 AM

0

Marc Heiler <shevegen@linuxmail.org> wrote:

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

--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
Leopard - http://www.takecontrolbooks.com/leopard-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...

Robert Klemme

6/6/2009 11:27:00 AM

0

On 06.06.2009 03:03, Marc Heiler wrote:
> 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

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestprac...

Marc Heiler

6/6/2009 5:32:00 PM

0

Thanks to all for the explanation!
--
Posted via http://www.ruby-....

Markus Fischer

6/6/2009 5:56:00 PM

0

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