[bug] Array#fetch weird second argument

  • Thread starter Simon Strandgaard
  • Start date
S

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.

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

Peter

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.

server> 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
 
S

Simon Strandgaard

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

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

Thanks for straighten this out.
 
D

Dave Thomas

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
 
S

Simon Strandgaard

Did you try 'ri Array.fetch?'
[snip ri-output]

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

Tobias Nurmiranta

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. ^_^
, Tobias
 
T

Tim Sutherland

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. ^_^
, Tobias

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

class Foo
def foo
end
end

You propose `Foo#foo' would be the same as `Foo.method:)foo)'. But the
latter does not work either.

What would `(Foo#foo).call' do? It needs an instance to call an instance
method :)
 
T

Tobias Nurmiranta

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

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. :)
, Tobias
 

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,145
Messages
2,570,824
Members
47,370
Latest member
desertedtyro29

Latest Threads

Top