Created
February 22, 2021 17:44
-
-
Save JoshCheek/70279df8ded876189f14a811f9a36b93 to your computer and use it in GitHub Desktop.
Tailcall optimization in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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