Issue #8811 has been updated by alexeymuranov (Alexey Muranov).


phluid61 (Matthew Kerwin) wrote:
> matz (Yukihiro Matsumoto) wrote:
> > Concrete use-case please?  Besides that, I don't think #index? is a good name for the function.
> > 
> > Matz.
> 
> I think the name is reasonable. Hash defines:
> * #key(value) => key of value
> * #key?(key)  => true if key present
> * #has_key?(key) => alias of #key?
> 
> Array defines:
> * #index(value) => index of value
> It makes sense that this method would be called #index? and/or #has_index?

There are two ways of using #index with an array: with non-negaitve integers, and with negaitve integers. It does not seem to make much sense to have an #index? method that returns true whenever an index fits one or the other convention. It seems to me indeed hard to come up with a use-case where the user wants to know if a number can be used as an index, but does not care if it is negative or positive. Two separate methods like #non_negative_index? and #negative_index? would look more natural but cumbersome to me.

----------------------------------------
Feature #8811: Counterpart to `Hash#key?` for `Array`
https://bugs.ruby-lang.org/issues/8811#change-41396

Author: sawa (Tsuyoshi Sawada)
Status: Feedback
Priority: Normal
Assignee: 
Category: 
Target version: 


=begin
`Hash` hash `key?` to tell if a key exists without checking the value. It would be convenient if there were a counterpart in `Array`. Suppose it is called `Array#index?`. Then it should behave as follows:

    [1, 2, 3].index?(2) # => true
    [1, 2, 3].index?(3) # => false
    [1, 2, 3].index?(-3) # => true
    [1, 2, 3].index?(-4) # => false

This is useful when we want to insert/move/delete elements to/from a certain position of an array. Without checking if a value exists, it can be messed up. Implementing a check is cumbersome now. With the proposed method, it would become easy.

=end



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