From: Dave Thomas Date: 2004-04-06T07:34:07+09:00 Subject: Re: [bug] Array#fetch weird second argument On Apr 5, 2004, at 14:14, Simon Strandgaard wrote: > While adding a testcase to rubicon for Array#fetch, I discovered that > #fetch takes a second argument. However I cannot figure out what its > purpose are. I have tried with lots of different input. > > It looks like a bug to me. Did you try 'ri Array.fetch?' ------------------------------------------------------------ Array#fetch array.fetch(index) => obj array.fetch(index, default ) => obj array.fetch(index) {|i| block } => obj ------------------------------------------------------------------------ Tries to return the element at position index. If the index lies outside the array, the first form throws an IndexError exception, the second form returns default, and the third form returns the value of invoking the block, passing in the index. Negative values of index count from the end of the array. a = [ 11, 22, 33, 44 ] a.fetch(1) #=> 22 a.fetch(-1) #=> 44 a.fetch(4, 'cat') #=> "cat" a.fetch(4) { |i| i*i } #=> 16 Cheers Dave