What are recursive arrays good for?

前端 未结 2 981
离开以前
离开以前 2020-12-03 01:26

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

a = []
# => [] 
a << a
# => [[...]] 
a.first == a
# => true 
2条回答
  •  死守一世寂寞
    2020-12-03 01:48

    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.

提交回复
热议问题