Skip to content

Instantly share code, notes, and snippets.

@edisonywh
Created November 30, 2018 09:18
Show Gist options
  • Save edisonywh/42b291d65476e9c60e34fb6557cb78a6 to your computer and use it in GitHub Desktop.
Save edisonywh/42b291d65476e9c60e34fb6557cb78a6 to your computer and use it in GitHub Desktop.
def traverse(tail)
head = list.shift
puts "Element: #{head}, Stack Count: #{caller.count}" #=> new line
return if tail.empty?
traverse(tail)
end
# Normal ruby
traverse(large_array)
# Element: 1, Stack Count: 1
# Element: ..., Stack Count: ...
# Element: ..., Stack Count: ...
# Element: 10918, Stack Count: 10918
# => stack level too deep (SystemStackError)
# TCO-enabled
traverse(large_array)
# Element: 1, Stack Count: 1
# Element: ..., Stack Count: 1
# Element: ..., Stack Count: 1
# Element: 500000, Stack Count: 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment