Issue #13692 has been updated by se8 (Sbastien Durand).


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

----------------------------------------
Feature #13692: Array#index?
https://bugs.ruby-lang.org/issues/13692#change-65522

* Author: se8 (Sbastien Durand)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
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.



~~~ ruby
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
~~~




-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>