Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created February 22, 2021 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshCheek/70279df8ded876189f14a811f9a36b93 to your computer and use it in GitHub Desktop.
Save JoshCheek/70279df8ded876189f14a811f9a36b93 to your computer and use it in GitHub Desktop.
Tailcall optimization in Ruby
# Remove the `-tco` from the end to run it without tailcall optimization
env RUBY_THREAD_VM_STACK_SIZE=100 ruby -e '
RubyVM::InstructionSequence.new(<<~RUBY, tailcall_optimization: !!$tco).eval
def fibo(n, a=0, b=1)
return a if n.zero?
fibo n-1, a+b, a
end
RUBY
p fibo 500' -s -- -tco
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment