441 views
What's the return value of the following Ruby code?
hash = {a: 1} hash[:b] = hash hash[:b][:b][:b][:a] # => ???
The correct answer is
nil
{a: 1, b: {...}}
1
SystemStackError
Subscribe to RubyCademy and get free access to all our courses, plus hundreds of fun Ruby cards, quizzes, guides, and tutorials!
hash = {a: 1} hash[:b] = hash hash[:b][:b][:b][:a] # => 1
hash = {a: 1}
At this point, the hash looks like this:
{a: 1}
:b
key to point to the hash itself:
hash[:b] = hash
Now the hash references itself under the key :b
, making it recursive:
{a: 1, b: {a: 1, b: {...}}}
The :b
key holds a reference to the original hash, which includes itself.
:b
key repeatedly:hash[:b][:b][:b][:a]
Here’s how Ruby interprets this:
hash[:b]
gives us the original hash.hash[:b][:b]
gives us the original hash again.hash[:b][:b][:b]
continues to return the original hash.So, every time we access :b
, we're just retrieving the original hash, which is {a: 1, b: {a: 1, b: {...}}}
.
:b
keys, we access the :a
key of the final hash:hash[:b][:b][:b][:a]
Since :a
was originally set to 1
, this entire chain of access ultimately returns:
hash[:b][:b][:b][:a] # => 1
Subscribe to RubyCademy and get free access to all our courses, plus hundreds of fun Ruby cards, quizzes, guides, and tutorials!
RubyCademy ©