[#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:81813] [Ruby trunk Feature#13692] Array#index?

From: shevegen@...
Date: 2017-06-28 17:57:23 UTC
List: ruby-core #81813
Issue #13692 has been updated by shevegen (Robert A. Heiler).


I am not sure if there is a huge need for it - and I am usually saying yes to lots of stuff. :)

I think another problem may be the name .index? e. g. what you expect - this depends a lot on
how matz feels about when he may read it.

In your example, your query is actually not on index but on "is in between this or that", so
the word .index? I think would be a misnomer, because Array already has #index and the behaviour
is different.

For example, you gave Hash#key and Hash#key?; these are a lot easier to understand than #index
and #index? in my opinion.

In particular for Hash, I always read .key? as ".has_key?". (At one point I think matz favoured
the shorter variant and there were perhaps deprecation warnings but since then it seems as if
the ruby core team also considers .has_key? as appropriate, and I love it... when I read 
if object.has_key? this_key, it makes me happy. :D )

Perhaps there may be another name for your functionality? .has_index? or .between_index? or something
like that?

For the functionality, I think it would help if you can provide some code example of where the
functionality is really useful (as applied in code) or more elegant or better than some other
functionality or so. A good example for this, in my opinion, was the suggestion for the lonely
operator (even though I don't use it); matz agreed that the query-checks were useful:

I can not find the discussion on the issue tracker but this blog entry shows a little summary
http://mitrev.net/ruby/2015/11/13/the-operator-in-ruby/ - though they call it "safe operator"...
I prefer lonely operator, I may even call it lonely duck operator... except that it is a person
rather than a duck. But I like ducks. :P Sorry for digressing, I guess in general, the better
your use case, the more likely it may be that matz and the ruby core team approves; you can see
some of this in other discussions that have met an official assignee.

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

* 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