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
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0