Forum: Ruby-core [ruby-trunk - Bug #6596][Open] New method for Arrays : Array#index

Posted by robin850 (Robin Dupret) (Guest)
on 2012-06-15 11:06
(Received via mailing list)
Issue #6596 has been reported by robin850 (Robin Dupret).

----------------------------------------
Bug #6596: New method for Arrays : Array#index
https://bugs.ruby-lang.org/issues/6596

Author: robin850 (Robin Dupret)
Status: Open
Priority: Normal
Assignee:
Category: core
Target version: 2.0.0
ruby -v: 2.0.0


Hello

5 days ago, I submitted a pull request on Github which provides a new 
method for the array objects which is Array#indexes. I have fist edit 
the Array#index method in order it to return an array of indexes and not 
a single index (which is the first occurrence it finds). I found it more 
logical but a user (trans) tells us that it could break the contract of 
Array#index so I decided to move it into Array#indexes. Eric (drbrain) 
tells me I should reasonning why I want to add this method ; it's just a 
point of view : I don't really understand why Array#index return a 
single index if the parameter is in the array several times.

Examples

a = [1, 2, 3, 1]
a.indexes(1)
Return : [0, 3]
a.index(1)
Return : 0
In my opinion, it's not really logical, 1 is in the array twice

Moreover, this pull request doesn't beak anything because we don't edit 
the Array#index method so programms which were created with previous 
version of Ruby will work.

I hope my post is complete. Have a nice day.
Posted by knu (Akinori MUSHA) (Guest)
on 2012-06-15 14:46
(Received via mailing list)
Issue #6596 has been updated by knu (Akinori MUSHA).


> a = [1, 2, 3, 1]
> a.indexes(1)
> Return : [0, 3]

Is there any proof that such a new functionality has a certain amount of 
need?

> a.index(1)
> Return : 0
> In my opinion, it's not really logical, 1 is in the array twice

We also have Array#rindex and it looks completely logical for us with 
some C background.
----------------------------------------
Bug #6596: New method for Arrays : Array#index
https://bugs.ruby-lang.org/issues/6596#change-27265

Author: robin850 (Robin Dupret)
Status: Open
Priority: Normal
Assignee:
Category: core
Target version: 2.0.0
ruby -v: 2.0.0


Hello

5 days ago, I submitted a pull request on Github which provides a new 
method for the array objects which is Array#indexes. I have fist edit 
the Array#index method in order it to return an array of indexes and not 
a single index (which is the first occurrence it finds). I found it more 
logical but a user (trans) tells us that it could break the contract of 
Array#index so I decided to move it into Array#indexes. Eric (drbrain) 
tells me I should reasonning why I want to add this method ; it's just a 
point of view : I don't really understand why Array#index return a 
single index if the parameter is in the array several times.

Examples

a = [1, 2, 3, 1]
a.indexes(1)
Return : [0, 3]
a.index(1)
Return : 0
In my opinion, it's not really logical, 1 is in the array twice

Moreover, this pull request doesn't beak anything because we don't edit 
the Array#index method so programms which were created with previous 
version of Ruby will work.

I hope my post is complete. Have a nice day.
Posted by robin850 (Robin Dupret) (Guest)
on 2012-06-15 17:10
(Received via mailing list)
Issue #6596 has been updated by robin850 (Robin Dupret).


Hello Knu,

Array#rindex return the last occurrence of the parameter. Array#indexes 
return all of the indexes.

> Is there any proof that such a new functionality has a certain amount of need?

Not really but I think it doesn't revoke anything, it's a good adding in 
my opinion.
----------------------------------------
Bug #6596: New method for Arrays : Array#index
https://bugs.ruby-lang.org/issues/6596#change-27267

Author: robin850 (Robin Dupret)
Status: Open
Priority: Normal
Assignee:
Category: core
Target version: 2.0.0
ruby -v: 2.0.0


Hello

5 days ago, I submitted a pull request on Github which provides a new 
method for the array objects which is Array#indexes. I have fist edit 
the Array#index method in order it to return an array of indexes and not 
a single index (which is the first occurrence it finds). I found it more 
logical but a user (trans) tells us that it could break the contract of 
Array#index so I decided to move it into Array#indexes. Eric (drbrain) 
tells me I should reasonning why I want to add this method ; it's just a 
point of view : I don't really understand why Array#index return a 
single index if the parameter is in the array several times.

Examples

a = [1, 2, 3, 1]
a.indexes(1)
Return : [0, 3]
a.index(1)
Return : 0
In my opinion, it's not really logical, 1 is in the array twice

Moreover, this pull request doesn't beak anything because we don't edit 
the Array#index method so programms which were created with previous 
version of Ruby will work.

I hope my post is complete. Have a nice day.
Posted by Thomas Sawyer (7rans)
on 2012-06-15 20:29
(Received via mailing list)
Issue #6596 has been updated by trans (Thomas Sawyer).


@kyu Try doing the equivalent of #indexes without it. Not that it's 
especially hard, but you have to stop and work out a solutuon. When you 
need it, that's when you wish there were already a method for it.

----------------------------------------
Bug #6596: New method for Arrays : Array#index
https://bugs.ruby-lang.org/issues/6596#change-27268

Author: robin850 (Robin Dupret)
Status: Open
Priority: Normal
Assignee:
Category: core
Target version: 2.0.0
ruby -v: 2.0.0


Hello

5 days ago, I submitted a pull request on Github which provides a new 
method for the array objects which is Array#indexes. I have fist edit 
the Array#index method in order it to return an array of indexes and not 
a single index (which is the first occurrence it finds). I found it more 
logical but a user (trans) tells us that it could break the contract of 
Array#index so I decided to move it into Array#indexes. Eric (drbrain) 
tells me I should reasonning why I want to add this method ; it's just a 
point of view : I don't really understand why Array#index return a 
single index if the parameter is in the array several times.

Examples

a = [1, 2, 3, 1]
a.indexes(1)
Return : [0, 3]
a.index(1)
Return : 0
In my opinion, it's not really logical, 1 is in the array twice

Moreover, this pull request doesn't beak anything because we don't edit 
the Array#index method so programms which were created with previous 
version of Ruby will work.

I hope my post is complete. Have a nice day.
Posted by Dan Connelly (djconnel)
on 2012-06-15 22:12
Thomas Sawyer wrote in post #1064758:
> Issue #6596 has been updated by trans (Thomas Sawyer).
>
>
> @kyu Try doing the equivalent of #indexes without it. Not that it's
> especially hard, but you have to stop and work out a solutuon. When you
> need it, that's when you wish there were already a method for it.
>

Seems fairly straightforward (for numeric indices with modest length
arrays) (pardon my simplicity here: I'm a beginner):

# definition
class Array
  def indexes(val)
    (0 ... self.length).select { |i| self[i] == val }
  end
end

# test
a = Array.new(10) { rand(3) }
puts a.join(", ")
puts a.indexes(0).join(", ")
puts a.indexes(1).join(", ")
puts a.indexes(2).join(", ")
Posted by knu (Akinori MUSHA) (Guest)
on 2012-06-16 09:42
(Received via mailing list)
Issue #6596 has been updated by knu (Akinori MUSHA).

Status changed from Open to Feedback

You haven't shown any real use case yet.
I don't deny that the functionality alone would be a good shortcut 
itself just like any other proposal we see every day.

But for example, if you just want to use it to delete the matching 
elements then it will be nothing better than Array#delete.
If I am to replace each element then I'll write a map!.with_index loop, 
or just use each_with_index in general.

It's all about how you would use it and what the supposed context would 
look like.
----------------------------------------
Bug #6596: New method for Arrays : Array#index
https://bugs.ruby-lang.org/issues/6596#change-27272

Author: robin850 (Robin Dupret)
Status: Feedback
Priority: Normal
Assignee:
Category: core
Target version: 2.0.0
ruby -v: 2.0.0


Hello

5 days ago, I submitted a pull request on Github which provides a new 
method for the array objects which is Array#indexes. I have fist edit 
the Array#index method in order it to return an array of indexes and not 
a single index (which is the first occurrence it finds). I found it more 
logical but a user (trans) tells us that it could break the contract of 
Array#index so I decided to move it into Array#indexes. Eric (drbrain) 
tells me I should reasonning why I want to add this method ; it's just a 
point of view : I don't really understand why Array#index return a 
single index if the parameter is in the array several times.

Examples

a = [1, 2, 3, 1]
a.indexes(1)
Return : [0, 3]
a.index(1)
Return : 0
In my opinion, it's not really logical, 1 is in the array twice

Moreover, this pull request doesn't beak anything because we don't edit 
the Array#index method so programms which were created with previous 
version of Ruby will work.

I hope my post is complete. Have a nice day.
Posted by Dan Connelly (djconnel)
on 2012-06-16 15:40
Okay... I'll try.

Suppose there's two arrays, the color of a bike, and how many seconds it
took for me to ride it up a hill.

So, for example:

color = [ :red, :black, :white, :black, :pink, ...]
secs = [42, 43, 44, 41, 40, 40 ...]

I might want to know if black bikes are faster than pink bikes..  so I
could write (ignoring issues of statistical significance):

puts "black bikes are faster!" \
  if (
    secs.values_at(*color.indexes(:black)).average  <
    secs.values_at(*color.indexes(:pink)).average
  )

Assuming an array object method "average".

But I could also do it this way (constraining to one line):
color.zip(secs).select{ |x| x[0] == :black }.map { |x| x[1] }.average #
=> 42
color.zip(secs).select{ |x| x[0] == :pink }.map { |x| x[1] }.average #=>
40

Or, what I proposed before (expanded):
secs.values_at(*(0 ... secs.length).select { |i| color[i] == :black
}).average  # => 42
secs.values_at(*(0 ... secs.length).select { |i| color[i] == :pink
}).average  # => 40

Perhaps there is a cleaner way (as I said, I'm new to Ruby).
Posted by Thomas Sawyer (7rans)
on 2012-06-16 23:27
(Received via mailing list)
Issue #6596 has been updated by trans (Thomas Sawyer).


=begin
@knu I think you have already pointed out the usecase. Wherever someone 
is using #each_with_index which contains a conditional selection would 
be a potential use case.

  even_indexes = []
  array_of_numbers.each_with_index do |c, i|
    next if c % 2 == 1
    even_indexes << i
  end

Basically #indexes is to #index as #select is to #find. We could do 
without #select too, but why would we?
=end

----------------------------------------
Bug #6596: New method for Arrays : Array#index
https://bugs.ruby-lang.org/issues/6596#change-27274

Author: robin850 (Robin Dupret)
Status: Feedback
Priority: Normal
Assignee:
Category: core
Target version: 2.0.0
ruby -v: 2.0.0


Hello

5 days ago, I submitted a pull request on Github which provides a new 
method for the array objects which is Array#indexes. I have fist edit 
the Array#index method in order it to return an array of indexes and not 
a single index (which is the first occurrence it finds). I found it more 
logical but a user (trans) tells us that it could break the contract of 
Array#index so I decided to move it into Array#indexes. Eric (drbrain) 
tells me I should reasonning why I want to add this method ; it's just a 
point of view : I don't really understand why Array#index return a 
single index if the parameter is in the array several times.

Examples

a = [1, 2, 3, 1]
a.indexes(1)
Return : [0, 3]
a.index(1)
Return : 0
In my opinion, it's not really logical, 1 is in the array twice

Moreover, this pull request doesn't beak anything because we don't edit 
the Array#index method so programms which were created with previous 
version of Ruby will work.

I hope my post is complete. Have a nice day.
Posted by knu (Akinori MUSHA) (Guest)
on 2012-06-17 04:25
(Received via mailing list)
Issue #6596 has been updated by knu (Akinori MUSHA).


@trans That's what it does, not a use case.  I am questioning you how it 
is useful to get an array of indices.
----------------------------------------
Bug #6596: New method for Arrays : Array#index
https://bugs.ruby-lang.org/issues/6596#change-27275

Author: robin850 (Robin Dupret)
Status: Feedback
Priority: Normal
Assignee:
Category: core
Target version: 2.0.0
ruby -v: 2.0.0


Hello

5 days ago, I submitted a pull request on Github which provides a new 
method for the array objects which is Array#indexes. I have fist edit 
the Array#index method in order it to return an array of indexes and not 
a single index (which is the first occurrence it finds). I found it more 
logical but a user (trans) tells us that it could break the contract of 
Array#index so I decided to move it into Array#indexes. Eric (drbrain) 
tells me I should reasonning why I want to add this method ; it's just a 
point of view : I don't really understand why Array#index return a 
single index if the parameter is in the array several times.

Examples

a = [1, 2, 3, 1]
a.indexes(1)
Return : [0, 3]
a.index(1)
Return : 0
In my opinion, it's not really logical, 1 is in the array twice

Moreover, this pull request doesn't beak anything because we don't edit 
the Array#index method so programms which were created with previous 
version of Ruby will work.

I hope my post is complete. Have a nice day.
Posted by Thomas Sawyer (7rans)
on 2012-06-17 15:40
(Received via mailing list)
Issue #6596 has been updated by trans (Thomas Sawyer).


=begin
https://github.com/apillet/invaders/blob/ab32d1704...

    @table.each_with_index do |row, rindex|
      row.each_with_index do |column, cindex|
        if column.nil? then
          @table[rindex][cindex] = enemy
          return
        end
      end
    end

Could have been written:

    @table.each_with_index do |row, rindex|
      row.indexes(nil).each do |cindex|
        @table[rindex][cindex] = enemy
        return
      end
    end

=end
----------------------------------------
Bug #6596: New method for Arrays : Array#index
https://bugs.ruby-lang.org/issues/6596#change-27279

Author: robin850 (Robin Dupret)
Status: Feedback
Priority: Normal
Assignee:
Category: core
Target version: 2.0.0
ruby -v: 2.0.0


Hello

5 days ago, I submitted a pull request on Github which provides a new 
method for the array objects which is Array#indexes. I have fist edit 
the Array#index method in order it to return an array of indexes and not 
a single index (which is the first occurrence it finds). I found it more 
logical but a user (trans) tells us that it could break the contract of 
Array#index so I decided to move it into Array#indexes. Eric (drbrain) 
tells me I should reasonning why I want to add this method ; it's just a 
point of view : I don't really understand why Array#index return a 
single index if the parameter is in the array several times.

Examples

a = [1, 2, 3, 1]
a.indexes(1)
Return : [0, 3]
a.index(1)
Return : 0
In my opinion, it's not really logical, 1 is in the array twice

Moreover, this pull request doesn't beak anything because we don't edit 
the Array#index method so programms which were created with previous 
version of Ruby will work.

I hope my post is complete. Have a nice day.
Posted by Nobuyoshi Nakada (nobu)
on 2012-06-17 16:03
(Received via mailing list)
Issue #6596 has been updated by nobu (Nobuyoshi Nakada).


Assuming the return value has no meanings.

  @table.any? do |row|
    if cindex = row.index(nil)
      row[cindex] = enemy
      true
    end
  end

----------------------------------------
Bug #6596: New method for Arrays : Array#index
https://bugs.ruby-lang.org/issues/6596#change-27280

Author: robin850 (Robin Dupret)
Status: Feedback
Priority: Normal
Assignee:
Category: core
Target version: 2.0.0
ruby -v: 2.0.0


Hello

5 days ago, I submitted a pull request on Github which provides a new 
method for the array objects which is Array#indexes. I have fist edit 
the Array#index method in order it to return an array of indexes and not 
a single index (which is the first occurrence it finds). I found it more 
logical but a user (trans) tells us that it could break the contract of 
Array#index so I decided to move it into Array#indexes. Eric (drbrain) 
tells me I should reasonning why I want to add this method ; it's just a 
point of view : I don't really understand why Array#index return a 
single index if the parameter is in the array several times.

Examples

a = [1, 2, 3, 1]
a.indexes(1)
Return : [0, 3]
a.index(1)
Return : 0
In my opinion, it's not really logical, 1 is in the array twice

Moreover, this pull request doesn't beak anything because we don't edit 
the Array#index method so programms which were created with previous 
version of Ruby will work.

I hope my post is complete. Have a nice day.
Posted by Thomas Sawyer (7rans)
on 2012-06-17 18:48
(Received via mailing list)
Issue #6596 has been updated by trans (Thomas Sawyer).


Yea, I would have written is completely different myself (even from 
yours). All I did was search GitHub for #each_with_index for code in 
which one could use #indexes. This was the first one I found. 
Unfortunately, as you can imagine, such a search is time consuming. 
Worse still GitHub's search turned out to have a bug and I could not go 
beyond page 100 of search results. There in lies one of the problem with 
"real" use case though --there's always dozens, if not more, way to do 
something. When you know you don't have a method at your disposal 
obviously you are going to think of ways to do it that won't require it.

So it can be very difficult to find a case until you are the one that 
realizes you could use it. And even when you do, lacking the method at 
hand, before any traction is made in any discussion like this you've 
already refactored in some fashion or anther to get things to work and 
have moved on. I know I've used a form of #indexes before but on a 
String, not an Array. And I remember being frustrated/surprised there 
was no method for it. But that was years ago now and I don't recall 
where it was.

The fact is, sometimes a method makes sense not b/c you have a shining 
example at hand, but b/c it fills an obvious hole in functionality. We 
have #index to find the first index. We have #rindex to find the last 
index. So it becomes obvious to ask about all the other indexes in 
between. Why do we have to fallback to writing our own loops to get at 
those? While I agree, it won't find a great deal of use, there are many 
methods like that. And yet it's nice that they are around when you do 
need them.

In considering the nature of this method further, I think the most 
quintessential use would be when you have two arrays corresponding by 
index and you want to locate the elements of one array based on the 
positions of a select elements of another. e.g.

    a1 = [:a, :b, :c, :b, :c]
    a2 = [:x, :z, :z, :y, :y]

    g[:b] = a2.indicies(*a1.select_indexes(:b))
    g[:b] #=> [:z, :y]

Yea, yea. Not a "real" use case. We all know code like that exists (and 
it pretty common back in the days of procedural coding). But I am not 
going to spend days searching through source code for it.

BTW, I just notice Enumerable#find_index was added to the language (in 
1.9.2?). So by analogy this could be Enumerable#select_index, or 
#select_indexes, right? So that why I used that in the last example.

----------------------------------------
Bug #6596: New method for Arrays : Array#index
https://bugs.ruby-lang.org/issues/6596#change-27281

Author: robin850 (Robin Dupret)
Status: Feedback
Priority: Normal
Assignee:
Category: core
Target version: 2.0.0
ruby -v: 2.0.0


Hello

5 days ago, I submitted a pull request on Github which provides a new 
method for the array objects which is Array#indexes. I have fist edit 
the Array#index method in order it to return an array of indexes and not 
a single index (which is the first occurrence it finds). I found it more 
logical but a user (trans) tells us that it could break the contract of 
Array#index so I decided to move it into Array#indexes. Eric (drbrain) 
tells me I should reasonning why I want to add this method ; it's just a 
point of view : I don't really understand why Array#index return a 
single index if the parameter is in the array several times.

Examples

a = [1, 2, 3, 1]
a.indexes(1)
Return : [0, 3]
a.index(1)
Return : 0
In my opinion, it's not really logical, 1 is in the array twice

Moreover, this pull request doesn't beak anything because we don't edit 
the Array#index method so programms which were created with previous 
version of Ruby will work.

I hope my post is complete. Have a nice day.
Posted by knu (Akinori MUSHA) (Guest)
on 2012-06-17 21:07
(Received via mailing list)
Issue #6596 has been updated by knu (Akinori MUSHA).


Since we introduced Enumerator (and Enumerable::Lazy recently) we are 
skeptic about adding a new method returning a complete array rather than 
one generating an enumeration, especially when a result array is 
unlikely ever to be used as a final result.
As I already said, generating an array just for iterating with it is 
nonsense.

If you start to say that #select_indexes may be what you want by now, 
then you haven't analyzed the desired feature enough.


I'd like to hear something from the originator before closing this 
issue.
----------------------------------------
Bug #6596: New method for Arrays : Array#index
https://bugs.ruby-lang.org/issues/6596#change-27282

Author: robin850 (Robin Dupret)
Status: Feedback
Priority: Normal
Assignee:
Category: core
Target version: 2.0.0
ruby -v: 2.0.0


Hello

5 days ago, I submitted a pull request on Github which provides a new 
method for the array objects which is Array#indexes. I have fist edit 
the Array#index method in order it to return an array of indexes and not 
a single index (which is the first occurrence it finds). I found it more 
logical but a user (trans) tells us that it could break the contract of 
Array#index so I decided to move it into Array#indexes. Eric (drbrain) 
tells me I should reasonning why I want to add this method ; it's just a 
point of view : I don't really understand why Array#index return a 
single index if the parameter is in the array several times.

Examples

a = [1, 2, 3, 1]
a.indexes(1)
Return : [0, 3]
a.index(1)
Return : 0
In my opinion, it's not really logical, 1 is in the array twice

Moreover, this pull request doesn't beak anything because we don't edit 
the Array#index method so programms which were created with previous 
version of Ruby will work.

I hope my post is complete. Have a nice day.
Posted by Thomas Sawyer (7rans)
on 2012-06-17 22:28
(Received via mailing list)
Issue #6596 has been updated by trans (Thomas Sawyer).


I don't see any reason it can't return an Enumerator.

My last example doesn't iterate over it.

#select_indexes is the exact same thing, just generalized to Enumerable 
instead of Array. You have to forgive me for not being omniscient, as I 
just spent the last two days "analyzing" this (in addition to the time I 
spent a few years back for String#index_all).

I doubt you will.

----------------------------------------
Bug #6596: New method for Arrays : Array#index
https://bugs.ruby-lang.org/issues/6596#change-27283

Author: robin850 (Robin Dupret)
Status: Feedback
Priority: Normal
Assignee:
Category: core
Target version: 2.0.0
ruby -v: 2.0.0


Hello

5 days ago, I submitted a pull request on Github which provides a new 
method for the array objects which is Array#indexes. I have fist edit 
the Array#index method in order it to return an array of indexes and not 
a single index (which is the first occurrence it finds). I found it more 
logical but a user (trans) tells us that it could break the contract of 
Array#index so I decided to move it into Array#indexes. Eric (drbrain) 
tells me I should reasonning why I want to add this method ; it's just a 
point of view : I don't really understand why Array#index return a 
single index if the parameter is in the array several times.

Examples

a = [1, 2, 3, 1]
a.indexes(1)
Return : [0, 3]
a.index(1)
Return : 0
In my opinion, it's not really logical, 1 is in the array twice

Moreover, this pull request doesn't beak anything because we don't edit 
the Array#index method so programms which were created with previous 
version of Ruby will work.

I hope my post is complete. Have a nice day.
Posted by knu (Akinori MUSHA) (Guest)
on 2012-06-18 05:26
(Received via mailing list)
Issue #6596 has been updated by knu (Akinori MUSHA).


=begin
You are changing the subject.  This issue is about Array#indexes, and if 
you talk about generalization, you should work out a concrete proposal 
with convincing examples to open another issue.

For that matter, I don't think "indexes" could or should be generalized 
to Enumerable because Enumerable currently does not have the sense of 
index.

Lastly, even if you sometimes need to collect indices you can simply use 
#select with ease:

    indexes = (0...a.size).select { |i| a[i] % 2 == 1 }

    #enumurator = (0...a.size).lazy.select { |i| a[i] % 2 == 1 }

Considering that indices are only useful when you access the originating 
array with them, the array should have a name and there should be no 
problem if you can't do this in a method chain.
=end

----------------------------------------
Bug #6596: New method for Arrays : Array#index
https://bugs.ruby-lang.org/issues/6596#change-27286

Author: robin850 (Robin Dupret)
Status: Feedback
Priority: Normal
Assignee:
Category: core
Target version: 2.0.0
ruby -v: 2.0.0


Hello

5 days ago, I submitted a pull request on Github which provides a new 
method for the array objects which is Array#indexes. I have fist edit 
the Array#index method in order it to return an array of indexes and not 
a single index (which is the first occurrence it finds). I found it more 
logical but a user (trans) tells us that it could break the contract of 
Array#index so I decided to move it into Array#indexes. Eric (drbrain) 
tells me I should reasonning why I want to add this method ; it's just a 
point of view : I don't really understand why Array#index return a 
single index if the parameter is in the array several times.

Examples

a = [1, 2, 3, 1]
a.indexes(1)
Return : [0, 3]
a.index(1)
Return : 0
In my opinion, it's not really logical, 1 is in the array twice

Moreover, this pull request doesn't beak anything because we don't edit 
the Array#index method so programms which were created with previous 
version of Ruby will work.

I hope my post is complete. Have a nice day.
Posted by Alex Young (regularfry)
on 2012-06-18 12:10
(Received via mailing list)
On 18/06/12 04:25, knu (Akinori MUSHA) wrote:
>
>      indexes = (0...a.size).select { |i| a[i] % 2 == 1 }
>
>      #enumurator = (0...a.size).lazy.select { |i| a[i] % 2 == 1 }
>
> Considering that indices are only useful when you access the originating array 
with them, the array should have a name and there should be no problem if you 
can't do this in a method chain.
> =end


Index lists *can* be useful without the original array.  A couple of
years ago I had the job of turning a big CSV into a database schema.
The first step in this was to work out which columns in the CSV are
correlated.  Here's a simplified example:

   $ cat example.csv
NAME,TYPE,LABEL
aleph,a,foo
aleph,b,foo
beth,a,bar

See how NAME and LABEL vary together?  I want them to end up in the same
table, with two rows in it.  You can group columns into tables by
generating an Array#indexes list for each unique entry in each column in
order, then seeing if the columns match by checking *just the index
lists* for for equality, ignoring the original column data.

--
Alex
Posted by Rodrigo Rosenfeld Rosas (Guest)
on 2012-06-18 16:45
(Received via mailing list)
Em 17-06-2012 13:47, trans (Thomas Sawyer) escreveu:
> Yea, I would have written is completely different myself (even from yours). All 
I did was search GitHub for #each_with_index for code in which one could use 
#indexes.

I guess you didn't get the idea of the policy here... Usually people
will come with suggestions on improvements to Ruby syntax or core
library through real issues taken from real programming on existing
projects.

We usually don't try to anticipate issues and use cases. We start with
them, so usually it is not hard to have a concrete example to
demonstrate how that feature would be useful...

If you're having to look at GitHub, it seems like you didn't find a
situation yourself where the requested feature would make you happier.

I would advice you not to attempt to find use cases beforehand, just let
them find you.
Posted by Thomas Sawyer (7rans)
on 2012-08-07 08:11
(Received via mailing list)
Issue #6596 has been updated by trans (Thomas Sawyer).


Given a ordered list, swap items relative to those that match.

  list.index_all{ |x| match?(x) }.each do |i|
    list[i], list[i+1] = list[i+1], list[i]
  end

vs. what?

  list.each_with_index do |x, i|
    if match?(x)
      list[i], list[i+1] = list[i+1], list[i]
    end
  end

Nope, that doesn't work. Could clone `list` but that would be rather 
inefficient. Better idea? A built-in #index_all is going to be a lot 
faster then a Ruby code implementation of the same.


----------------------------------------
Bug #6596: New method for Arrays : Array#index
https://bugs.ruby-lang.org/issues/6596#change-28701

Author: robin850 (Robin Dupret)
Status: Feedback
Priority: Normal
Assignee:
Category: core
Target version: 2.0.0
ruby -v: 2.0.0


Hello

5 days ago, I submitted a pull request on Github which provides a new 
method for the array objects which is Array#indexes. I have fist edit 
the Array#index method in order it to return an array of indexes and not 
a single index (which is the first occurrence it finds). I found it more 
logical but a user (trans) tells us that it could break the contract of 
Array#index so I decided to move it into Array#indexes. Eric (drbrain) 
tells me I should reasonning why I want to add this method ; it's just a 
point of view : I don't really understand why Array#index return a 
single index if the parameter is in the array several times.

Examples

a = [1, 2, 3, 1]
a.indexes(1)
Return : [0, 3]
a.index(1)
Return : 0
In my opinion, it's not really logical, 1 is in the array twice

Moreover, this pull request doesn't beak anything because we don't edit 
the Array#index method so programms which were created with previous 
version of Ruby will work.

I hope my post is complete. Have a nice day.
Posted by ko1 (Koichi Sasada) (Guest)
on 2012-10-27 02:25
(Received via mailing list)
Issue #6596 has been updated by ko1 (Koichi Sasada).

Assignee set to mame (Yusuke Endoh)

Is it a bug or a feature?

----------------------------------------
Bug #6596: New method for Arrays : Array#index
https://bugs.ruby-lang.org/issues/6596#change-31726

Author: robin850 (Robin Dupret)
Status: Feedback
Priority: Normal
Assignee: mame (Yusuke Endoh)
Category: core
Target version: 2.0.0
ruby -v: 2.0.0


Hello

5 days ago, I submitted a pull request on Github which provides a new 
method for the array objects which is Array#indexes. I have fist edit 
the Array#index method in order it to return an array of indexes and not 
a single index (which is the first occurrence it finds). I found it more 
logical but a user (trans) tells us that it could break the contract of 
Array#index so I decided to move it into Array#indexes. Eric (drbrain) 
tells me I should reasonning why I want to add this method ; it's just a 
point of view : I don't really understand why Array#index return a 
single index if the parameter is in the array several times.

Examples

a = [1, 2, 3, 1]
a.indexes(1)
Return : [0, 3]
a.index(1)
Return : 0
In my opinion, it's not really logical, 1 is in the array twice

Moreover, this pull request doesn't beak anything because we don't edit 
the Array#index method so programms which were created with previous 
version of Ruby will work.

I hope my post is complete. Have a nice day.
Posted by mame (Yusuke Endoh) (Guest)
on 2012-10-27 11:59
(Received via mailing list)
Issue #6596 has been updated by mame (Yusuke Endoh).

Status changed from Feedback to Assigned
Assignee changed from mame (Yusuke Endoh) to matz (Yukihiro Matsumoto)
Target version changed from 2.0.0 to next minor

ko1 (Koichi Sasada) wrote:
> Is it a bug or a feature?

I think it is a feature, in every respect.

--
Yusuke Endoh <mame@tsg.ne.jp>
----------------------------------------
Bug #6596: New method for Arrays : Array#index
https://bugs.ruby-lang.org/issues/6596#change-31786

Author: robin850 (Robin Dupret)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version: next minor
ruby -v: 2.0.0


Hello

5 days ago, I submitted a pull request on Github which provides a new 
method for the array objects which is Array#indexes. I have fist edit 
the Array#index method in order it to return an array of indexes and not 
a single index (which is the first occurrence it finds). I found it more 
logical but a user (trans) tells us that it could break the contract of 
Array#index so I decided to move it into Array#indexes. Eric (drbrain) 
tells me I should reasonning why I want to add this method ; it's just a 
point of view : I don't really understand why Array#index return a 
single index if the parameter is in the array several times.

Examples

a = [1, 2, 3, 1]
a.indexes(1)
Return : [0, 3]
a.index(1)
Return : 0
In my opinion, it's not really logical, 1 is in the array twice

Moreover, this pull request doesn't beak anything because we don't edit 
the Array#index method so programms which were created with previous 
version of Ruby will work.

I hope my post is complete. Have a nice day.
Posted by zzak (Zachary Scott) (Guest)
on 2012-11-19 01:35
(Received via mailing list)
Issue #6596 has been updated by zzak (Zachary Scott).

% Done changed from 100 to 0


----------------------------------------
Feature #6596: New method for Arrays : Array#index
https://bugs.ruby-lang.org/issues/6596#change-33073

Author: robin850 (Robin Dupret)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version: next minor


Hello

5 days ago, I submitted a pull request on Github which provides a new 
method for the array objects which is Array#indexes. I have fist edit 
the Array#index method in order it to return an array of indexes and not 
a single index (which is the first occurrence it finds). I found it more 
logical but a user (trans) tells us that it could break the contract of 
Array#index so I decided to move it into Array#indexes. Eric (drbrain) 
tells me I should reasonning why I want to add this method ; it's just a 
point of view : I don't really understand why Array#index return a 
single index if the parameter is in the array several times.

Examples

a = [1, 2, 3, 1]
a.indexes(1)
Return : [0, 3]
a.index(1)
Return : 0
In my opinion, it's not really logical, 1 is in the array twice

Moreover, this pull request doesn't beak anything because we don't edit 
the Array#index method so programms which were created with previous 
version of Ruby will work.

I hope my post is complete. Have a nice day.
Posted by robin850 (Robin Dupret) (Guest)
on 2012-11-19 19:15
(Received via mailing list)
Issue #6596 has been updated by robin850 (Robin Dupret).


Any new on this please ? I see that this ticket is assigned to Matz. Is 
there any chance to see it implemented in ruby ?
----------------------------------------
Feature #6596: New method for Arrays : Array#index
https://bugs.ruby-lang.org/issues/6596#change-33118

Author: robin850 (Robin Dupret)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version: next minor


Hello

5 days ago, I submitted a pull request on Github which provides a new 
method for the array objects which is Array#indexes. I have fist edit 
the Array#index method in order it to return an array of indexes and not 
a single index (which is the first occurrence it finds). I found it more 
logical but a user (trans) tells us that it could break the contract of 
Array#index so I decided to move it into Array#indexes. Eric (drbrain) 
tells me I should reasonning why I want to add this method ; it's just a 
point of view : I don't really understand why Array#index return a 
single index if the parameter is in the array several times.

Examples

a = [1, 2, 3, 1]
a.indexes(1)
Return : [0, 3]
a.index(1)
Return : 0
In my opinion, it's not really logical, 1 is in the array twice

Moreover, this pull request doesn't beak anything because we don't edit 
the Array#index method so programms which were created with previous 
version of Ruby will work.

I hope my post is complete. Have a nice day.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.