Skip to content

Instantly share code, notes, and snippets.

@pocari
Created September 27, 2016 07:04
Show Gist options
  • Save pocari/031a6da96c066badc5fe3abb62fab15b to your computer and use it in GitHub Desktop.
Save pocari/031a6da96c066badc5fe3abb62fab15b to your computer and use it in GitHub Desktop.
ruby's tailcall optimization
RubyVM::InstructionSequence.compile_option = {
tailcall_optimization: true,
trace_instruction: false
}
RubyVM::InstructionSequence.compile(<<EOS).eval
def factorial(n, acc = 1)
if n == 0
acc
else
factorial(n - 1, acc * n)
end
end
EOS
p factorial(100000).to_s.size
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment