Project

General

Profile

Actions

Feature #8811

closed

Counterpart to `Hash#key?` for `Array`

Added by sawa (Tsuyoshi Sawada) over 10 years ago. Updated over 10 years ago.

Status:
Feedback
Assignee:
-
Target version:
-
[ruby-core:56784]

Description

=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


Files

rb_ary_has_index.patch (2.08 KB) rb_ary_has_index.patch Simple implementation with test phluid61 (Matthew Kerwin), 08/27/2013 09:04 AM

Updated by matz (Yukihiro Matsumoto) over 10 years ago

  • Status changed from Open to Feedback

Concrete use-case please? Besides that, I don't think #index? is a good name for the function.

Matz.

Updated by phluid61 (Matthew Kerwin) over 10 years ago

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?

Updated by alexeymuranov (Alexey Muranov) over 10 years ago

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.

Updated by zzak (zzak _) over 10 years ago

Shouldn't it be called Hash#value then?

Updated by zzak (zzak _) over 10 years ago

Ahh, sorry I misunderstood the feature request.

Request is for Array#index?

Updated by sawa (Tsuyoshi Sawada) over 10 years ago

=begin

  • Matz: As for the method name, phluid61's explanation is exactly what I had in mind. And, some use cases are as shown below.
  • phluid61: Your implementation looks perfect to me.

==== Use case 1
I want to delete and get from an array a an element at position i if there is such element. That element can be possibly nil.

With index?, it works like this:

a = ["a", nil, "b"]
deleted = :none
deleted = a.delete_at(3) if a.index?(3) # => Deletion did not happen.
deleted # => :none # I can be sure that deletion did not happen.

a = ["a", nil, "b"]
deleted = :none
deleted = a.delete_at(1) if a.index?(1) # => Deleted `nil`.
deleted # => nil # I can be sure that an element `nil` was deleted.

Without checking the index, I cannot tell whether deletion happened:

a = ["a", nil, "b"]
deleted = :none
deleted = a.delete_at(3) # => Deletion did not happen.
deleted # => nil # I cannot tell whether an element `nil` was deleted or deletion did not happen.

a = ["a", nil, "b"]
deleted = :none
deleted = a.delete_at(1) if a.index?(1) # => Deleted `nil`.
deleted # => nil # I cannot tell whether an element `nil` was deleted or deletion did not happen.

=== Use case 2
I want to prepend with :foo an element at position i if there is such element.

With index?, it works like this:

a = ["a", "b"]
a.insert(1, :foo) if a.index?(1)
# => ["a", :foo, "b"]

a = ["a", "b"]
a.insert(3, :foo) if a.index?(3)
# => ["a", "b"]

Without index?, unwanted nil will be inserted

a = ["a", "b"]
a.insert(1, :foo)
# => ["a", :foo, "b"] # This is okay.

a = ["a", "b"]
a.insert(3, :foo)
# => ["a", "b", nil, :foo] # Unwanted result

=end

Updated by alexeymuranov (Alexey Muranov) over 10 years ago

Why not to use

3 < a.size

instead of

a.index?(3)

etc.?

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0