[bug] Array#fetch weird second argument

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.

irb
irb(main):001:0> %w(a b c).fetch(1, 500)
=> “b”
irb(main):002:0> %w(a b c).fetch(1, -9)
=> “b”
irb(main):003:0> %w(a b c).fetch(1, nil)
=> “b”
irb(main):004:0>

It looks like a bug to me.

···


Simon Strandgaard

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.

irb
irb(main):001:0> %w(a b c).fetch(1, 500)
=> “b”
irb(main):002:0> %w(a b c).fetch(1, -9)
=> “b”
irb(main):003:0> %w(a b c).fetch(1, nil)
=> “b”

irb(main):004:0> %w(a b c).fetch(10)
IndexError: index 10 out of array
from (irb):10:in `fetch’
from (irb):10
irb(main):005:0> %w(a b c).fetch(10, 500)
=> 500

Peter

···

from :0

Did you try ‘ri Array.fetch?’

------------------------------------------------------------ Array#fetch
array.fetch(index) => obj
array.fetch(index, default ) => obj
array.fetch(index) {|i| block } => obj

···

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.


  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

Ah… Bommer. I see the second argument is the default value.

Thanks for straighten this out.

···

On Tue, 06 Apr 2004 05:23:07 +0900, Peter 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.

irb
irb(main):001:0> %w(a b c).fetch(1, 500)
=> “b”
irb(main):002:0> %w(a b c).fetch(1, -9)
=> “b”
irb(main):003:0> %w(a b c).fetch(1, nil)
=> “b”

irb(main):004:0> %w(a b c).fetch(10)
IndexError: index 10 out of array
from (irb):10:in `fetch’
from (irb):10
from :0
irb(main):005:0> %w(a b c).fetch(10, 500)
=> 500

Peter


Simon Strandgaard

[snip ri-output]

Yes, but then it was too late… I had already posted this message :slight_smile:
I must admit that Im not that used to ‘ri’… I keep forgetting.

BTW: ri is nice.
BTW2: I have added some tests of ruby-1.8’s new methods in rubicon.

···

On Tue, 06 Apr 2004 08:34:07 +0900, Dave Thomas wrote:

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?’

Simon Strandgaard

Hi,

I often see the SmallTalkish syntax Class#method in texts in this forum,
as the subject in this mail. Wouldn’t it be nice if it was part of the
Ruby syntax as well, not just the “discussion syntax”. So instead of
method(:foo) you could write Class#foo or self#foo, or maybe even #foo.

But since the “#” is used for comments, it might get a bit ambigious. :slight_smile:
, Tobias

Hmm, maybe it wasn’t so thought through :). It will probably just
confuse with semantics like this:

class C
def f()
3
end

def C.g()
4
end
end

x = C.new
y = x#f # same as x.method(:f)
y.call => 3

a = C#g # same as C.method(:g)
a.call => 4

Ehm, well forget it. :slight_smile:
, Tobias

···

On Thu, 15 Apr 2004, Tim Sutherland wrote:

In article Pine.GSO.4.51L2.0404150105460.5004@nazgul.lysator.liu.se, Tobias
Nurmiranta wrote:

I often see the SmallTalkish syntax Class#method in texts in this forum,
as the subject in this mail. Wouldn’t it be nice if it was part of the
Ruby syntax as well, not just the “discussion syntax”. So instead of
method(:foo) you could write Class#foo or self#foo, or maybe even #foo.

But since the “#” is used for comments, it might get a bit ambigious. :slight_smile:
, Tobias

Class#foo means an instance method foo' of the class Class’.