Project

General

Profile

Actions

Feature #13692

closed

Array#index?

Added by Anonymous almost 7 years ago. Updated almost 7 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:81808]

Description

Hello!

We currently have these methods:

Hash#key(value)
Hash#key?(key)
Array#index(value)

I was thinking we could add this tiny method:

Array#index?(index)

as this is sometimes useful to check if an array index exists.

class Array

  def index?(index)
    index.between?(0 - size, size - 1)
  end

end

['a', 'b', 'c'].index?(0) # => true
['a', 'b', 'c'].index?(2) # => true
['a', 'b', 'c'].index?(3) # => false
['a', 'b', 'c'].index?(-1) # => true
['a', 'b', 'c'].index?(-3) # => true
['a', 'b', 'c'].index?(-4) # => false
[false, nil].index?(0) #=> true
[false, nil].index?(1) #=> true

Updated by Anonymous almost 7 years ago

Or is this feature too trivial ?

Updated by k0kubun (Takashi Kokubun) almost 7 years ago

this is sometimes useful to check if an array index exists.

I couldn't imagine the situation that needs to check if an array index exists. Could you show a use case that needs array index check?

Updated by shevegen (Robert A. Heiler) almost 7 years ago

I am not sure if there is a huge need for it - and I am usually saying yes to lots of stuff. :)

I think another problem may be the name .index? e. g. what you expect - this depends a lot on
how matz feels about when he may read it.

In your example, your query is actually not on index but on "is in between this or that", so
the word .index? I think would be a misnomer, because Array already has #index and the behaviour
is different.

For example, you gave Hash#key and Hash#key?; these are a lot easier to understand than #index
and #index? in my opinion.

In particular for Hash, I always read .key? as ".has_key?". (At one point I think matz favoured
the shorter variant and there were perhaps deprecation warnings but since then it seems as if
the ruby core team also considers .has_key? as appropriate, and I love it... when I read
if object.has_key? this_key, it makes me happy. :D )

Perhaps there may be another name for your functionality? .has_index? or .between_index? or something
like that?

For the functionality, I think it would help if you can provide some code example of where the
functionality is really useful (as applied in code) or more elegant or better than some other
functionality or so. A good example for this, in my opinion, was the suggestion for the lonely
operator (even though I don't use it); matz agreed that the query-checks were useful:

I can not find the discussion on the issue tracker but this blog entry shows a little summary
http://mitrev.net/ruby/2015/11/13/the-operator-in-ruby/ - though they call it "safe operator"...
I prefer lonely operator, I may even call it lonely duck operator... except that it is a person
rather than a duck. But I like ducks. :P Sorry for digressing, I guess in general, the better
your use case, the more likely it may be that matz and the ruby core team approves; you can see
some of this in other discussions that have met an official assignee.

Updated by Anonymous almost 7 years ago

This feature may not be that good, I agree. :)

I thought it could occasionally be useful when querying arrays. For example, we have this method in Ruby Facets:

https://github.com/rubyworks/facets/blob/master/lib/core/facets/array/from.rb

def from(index)
    return [] if index >= size
    self[index..-1]
end

Here the problem is that this method uses "index >= size" to check if the index exists. It's ok as long as we use positive indexes, but it doesn't work with negative indexes:

[0, 1, 2].from(1) # => [1, 2]
[0, 1, 2].from(10) # => []
[0, 1, 2].from(-10) # => nil - and I was expecting an empty array to chain other methods

Relying on index? solves that problem:

def from(index)
  if index?(index)
     self[index..-1]
  else
    []
  end
end
[0, 1, 2].from(-10) # => []

Also, it could be nice to have similar Hash and Array features:

key?(key) # => true or false
index?(index) # => true or false
key(value) # => a key
index(value) # => an index

But yes, Array is not a numerical Hash and is not designed to be used that way:

array = []
array[42] = 'Hello World !'
array.size # => 43

So I don't know really. It's not a life changing method for sure! :)

Updated by k0kubun (Takashi Kokubun) almost 7 years ago

def from(index)
  if index?(index)
     self[index..-1]
  else
    []
  end
end

Not considered well, but wouldn't it be sufficient to use following simple implementation?

def from(index)
  self[index..-1] || []
end

And first of all, I couldn't imagine the use case to use such Array#from with large negative index too.

Updated by Anonymous almost 7 years ago

Yes, I think you are right. This probably was a bad idea. An array is not a (numerical) Hash.

So a moderator can close this ticket.

Thanks for your help, have a good continuation. :)

Actions #7

Updated by k0kubun (Takashi Kokubun) almost 7 years ago

  • Status changed from Open to Rejected
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0