Comment on this RCR (edit wiki page) | RCRchive home

RCR 236: anArray.rekursive_index

submitted by Icekiss on Sat Mar 27 2004 01:53:19 PM -0800

Status: pending


Abstract

Add a new instance method to the module Enumerable to allow a more powerfull way to search for a key.

Problem

It isn't currently trivially possible to search for a key in an Enumerable that contains subenumerables.

Proposal

Add a new instance method rekursive_index to the module Enumerable. Interface:
rekursive_index(key) -> anArray
This method would return an array, that contains the indexes of the Enumerables necessary to reach the key.
example:
a=[9, 7, 5, [1, 2], 3]
a.rekursive_index(1) -> [3, 0]
a.rekursive_index(7) -> [1]
a.rekursive_index(0) -> nil
a.rekursive_index([1,2]) -> [3]

Analysis

It isn't possible to just reimplement the index method to achieve this, because rekursive_index will return an Array with one element, where index would just return the element. And for consistencies sake, it has to be that way.

Implementation

module Enumerable
        def rekursive_index(key)
                each_with_index do |element, index|
                        return [index] if element==key
                        #if element.
                        if element.kind_of?(Enumerable)
                                rIndex=element.rekursive_index(key)
                                return [index]+rIndex if rIndex
                        end
                end
                return nil
        end
end

Vote for this RCR

Strongly opposed [2]
Opposed [2]
Neutral [0]
In favor [0]
Strongly advocate [0]

Change the status of this RCR to:

accepted

rejected

withdrawn


Add comments here

Since I have superseded the proposal I made just a few hours ago, I will repeat my short comment to it here:
<quote> I have discovered ruby 3 days ago, and I have to say: It is absolutely the best programming language I have ever seen. Just thought I share what usefull additions to the standard classes I can see. :-)

And a deep bow to matz for making this language available. *bow* </quote>

The reason I superseded is obvious: It should of course be implemented on the level of enumberation. Is it called a module instance method? "Programming Ruby" contradicts itself on the naming...
--Icekiss

I'm getting a bit confused: I reread the list of methods implemented by the various classes: Lots of things that could be done for every enumerable (e.g index) are implemented only for arrays. Any reason for this? To implement this proposal only for arrays, just replace:
module Enumerable
with
class Array
and
if element.kind_of?(Enumerable)
with
if element.kind_of?(Array)
--Icekiss


Icekiss, you might want to slow down and keep studying and learning about Ruby for a while. There's no shame attached to taking more than three days to suggest languages changes :-) You can ask questions, and learn a lot, on the ruby-talk mailing list and also on the IRC channel #ruby-lang, on irc.freenode.net. -- David Black

Back to RCRchive.


RCR Submission page and RCRchive powered by Ruby, Apache, RuWiki (modified), and RubLog