[#81492] [Ruby trunk Feature#13618] [PATCH] auto fiber schedule for rb_wait_for_single_fd and rb_waitpid — normalperson@...

Issue #13618 has been reported by normalperson (Eric Wong).

12 messages 2017/06/01

[ruby-core:81810] [Ruby trunk Feature#13692] Array#index?

From: takashikkbn@...
Date: 2017-06-28 15:35:22 UTC
List: ruby-core #81810
Issue #13692 has been updated by k0kubun (Takashi Kokubun).


> 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?

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

* Author: se8 (S辿bastien 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>

In This Thread

Prev Next