Take the 2-minute tour ×

Ruby supports recursive arrays (that is, self-containing arrays):

a = []
# => [] 
a << a
# => [[...]] 
a.first == a
# => true 

This is intrinsically cool, but what work can you do with it?

share|improve this question
Note that this example is easy, since a.first and a are exactly the same object (same object_id). Ruby supports comparison of independent recursive structures too (e.g. b = []; b << b; a == b # => true). –  Marc-André Lafortune Sep 7 '12 at 13:53

2 Answers 2

up vote 41 down vote accepted

A directed graph with undifferentiated edges could have each vertex represented simply as an array of the the vertices reachable from that vertex. If the graph had cycles, you would have a 'recursive array', especially if an edge could lead back to the same vertex.

For example, this graph:
directed cyclic graph
...could be represented in code as:

nodes = { a:[], b:[], c:[], d:[] }
nodes[:a] << nodes[:a]
nodes[:a] << nodes[:b]
nodes[:b] << nodes[:a]
nodes[:b] << nodes[:c]
p nodes
#=> {:a=>[[[...], []], [...]], :b=>[[[...], [...]], []], :c=>[], :d=>[]}

Usually the representation of each vertex would be more 'robust' (e.g. a class instance with properties for the name and array of outgoing edges), but it's not impossible to imagine a case where you wanted a very lightweight representation of your data (for very large graphs) and so needed to use a minimal representation like this.

share|improve this answer
But what application does this possibly have? –  Simpleton Sep 6 '12 at 21:40
@Simpleton What applications do directed graphs have? Lots! Route finding; establishing a hierarchy of cell-based formulae dependencies to name two. –  Phrogz Sep 6 '12 at 21:51
Cannot you do the exact same thing in just about any language that supports reference elements in lists? –  inger Nov 27 '13 at 21:23
@inger Yes, of course. Recursive, self-referential data structures are certainly not limited to Ruby. That just happened to be what the OP was asking about. –  Phrogz Nov 27 '13 at 21:28

Ruby supports recursive arrays

To me the question is why should it not support it?

An Array is simply a collection of references. Should it check each element and throw an error if one of the refers to the collection itself, so prevent recursion or using it for graphs like Phrogz' example.

So I don't think it's a feature, but if it would be, most languages I know has it, even Java.. Just use Object as Array elements.

share|improve this answer
I'm not aware of another language besides Ruby that strongly supports recursive arrays. For example, I don't think you can compare two distinct recursive arrays in any other language. In Ruby: a = []; a << a; b = []; b << b; a == b # => true –  Marc-André Lafortune Sep 7 '12 at 13:48
@Marc-André right that's an interesting corner-case, not sure who many other languages have that level of support. Looking at the question/example I was (mis?)interpreting the OP's notion of 'support' as it allows you creating recursive arrays - which most language does AFAIK. –  inger Sep 9 '12 at 22:18

Your Answer


