https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112013-08-23T13:20:27ZRuby Issue Tracking SystemRuby master - Feature #8811: Counterpart to `Hash#key?` for `Array`https://redmine.ruby-lang.org/issues/8811?journal_id=413302013-08-23T13:20:27Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Feedback</i></li></ul><p>Concrete use-case please? Besides that, I don't think #index? is a good name for the function.</p>
<p>Matz.</p> Ruby master - Feature #8811: Counterpart to `Hash#key?` for `Array`https://redmine.ruby-lang.org/issues/8811?journal_id=413582013-08-27T09:04:21Zphluid61 (Matthew Kerwin)matthew@kerwin.net.au
<ul><li><strong>File</strong> <a href="/attachments/3913">rb_ary_has_index.patch</a> <a class="icon-only icon-download" title="Download" href="/attachments/download/3913/rb_ary_has_index.patch">rb_ary_has_index.patch</a> added</li></ul><p>matz (Yukihiro Matsumoto) wrote:</p>
<blockquote>
<p>Concrete use-case please? Besides that, I don't think #index? is a good name for the function.</p>
<p>Matz.</p>
</blockquote>
<p>I think the name is reasonable. Hash defines:</p>
<ul>
<li>#key(value) => key of value</li>
<li>#key?(key) => true if key present</li>
<li>#has_key?(key) => alias of #key?</li>
</ul>
<p>Array defines:</p>
<ul>
<li>#index(value) => index of value<br>
It makes sense that this method would be called #index? and/or #has_index?</li>
</ul> Ruby master - Feature #8811: Counterpart to `Hash#key?` for `Array`https://redmine.ruby-lang.org/issues/8811?journal_id=413962013-08-29T06:14:24Zalexeymuranov (Alexey Muranov)
<ul></ul><p>phluid61 (Matthew Kerwin) wrote:</p>
<blockquote>
<p>matz (Yukihiro Matsumoto) wrote:</p>
<blockquote>
<p>Concrete use-case please? Besides that, I don't think #index? is a good name for the function.</p>
<p>Matz.</p>
</blockquote>
<p>I think the name is reasonable. Hash defines:</p>
<ul>
<li>#key(value) => key of value</li>
<li>#key?(key) => true if key present</li>
<li>#has_key?(key) => alias of #key?</li>
</ul>
<p>Array defines:</p>
<ul>
<li>#index(value) => index of value<br>
It makes sense that this method would be called #index? and/or #has_index?</li>
</ul>
</blockquote>
<p>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.</p> Ruby master - Feature #8811: Counterpart to `Hash#key?` for `Array`https://redmine.ruby-lang.org/issues/8811?journal_id=415502013-09-03T13:05:11Zzzak (zzak _)
<ul></ul><p>Shouldn't it be called Hash#value then?</p> Ruby master - Feature #8811: Counterpart to `Hash#key?` for `Array`https://redmine.ruby-lang.org/issues/8811?journal_id=415512013-09-03T13:06:46Zzzak (zzak _)
<ul></ul><p>Ahh, sorry I misunderstood the feature request.</p>
<p>Request is for Array#index?</p> Ruby master - Feature #8811: Counterpart to `Hash#key?` for `Array`https://redmine.ruby-lang.org/issues/8811?journal_id=415762013-09-03T21:27:43Zsawa (Tsuyoshi Sawada)
<ul></ul><p>=begin</p>
<ul>
<li>Matz: As for the method name, phluid61's explanation is exactly what I had in mind. And, some use cases are as shown below.</li>
<li>phluid61: Your implementation looks perfect to me.</li>
</ul>
<p>==== Use case 1<br>
I want to delete and get from an array <code>a</code> an element at position <code>i</code> if there is such element. That element can be possibly <code>nil</code>.</p>
<p>With <code>index?</code>, it works like this:</p>
<pre><code>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.
</code></pre>
<p>Without checking the index, I cannot tell whether deletion happened:</p>
<pre><code>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.
</code></pre>
<p>=== Use case 2<br>
I want to prepend with <code>:foo</code> an element at position <code>i</code> if there is such element.</p>
<p>With <code>index?</code>, it works like this:</p>
<pre><code>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"]
</code></pre>
<p>Without <code>index?</code>, unwanted <code>nil</code> will be inserted</p>
<pre><code>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
</code></pre>
<p>=end</p> Ruby master - Feature #8811: Counterpart to `Hash#key?` for `Array`https://redmine.ruby-lang.org/issues/8811?journal_id=415852013-09-04T02:12:58Zalexeymuranov (Alexey Muranov)
<ul></ul><p>Why not to use</p>
<p>3 < a.size</p>
<p>instead of</p>
<p>a.index?(3)</p>
<p>etc.?</p>