Somebody tell me what’s going on here:
a = [0,1,2]
a.each {|x| a[x] = a}
The result is [[...], [...], [...]]. And if I evaluate a[0] I get [[...], [...], [...]]. And if I evaluate a[0][0] I get [[...], [...], [...]] ad infinitum.
Have I created an array of infinite dimensionality? How/Why should this possibly work?
Basically you’ve modified every element in
ato reference the list itself. The list is recursively referencing itself:(
# =>is a Rubyism for “this line evaluates to”)Depending on how you look at it it is not infinite. It’s more or less just like a piece of paper with the words “please turn over” written on both sides.
The reason that Ruby prints
[...]is that it is clever enough to discover that the list is recursive, and avoids going into an infinite loop.By the way, your usage of
eachis a bit non-idiomatic.eachreturns the list, and you usually don’t assign this return value to a variable (since you already have a variable referencing it,ain this case). In other words, your code assigns[0,1,2]toa, then loops overa(setting each element toa), then assignsatoa.