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>
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
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
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
Yes, but then it was too late… I had already posted this message
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.
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.
, Tobias
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.
, Tobias
Class#foo means an instance method foo' of the class Class’.