Skip to content

Instantly share code, notes, and snippets.

@tdg5
Created March 21, 2015 16:59
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 tdg5/c6a39168b3ac252b812b to your computer and use it in GitHub Desktop.
Save tdg5/c6a39168b3ac252b812b to your computer and use it in GitHub Desktop.
Instance variable reference from tail recursive call
require "tco_method"
class TCOInstance
extend TCOMethod::Mixin
attr_reader :tco_cycles
def initialize
@tco_cycles = 0
end
def tco_instance_var_referrer(n, acc = 1)
@tco_cycles += 1
n < 2 ? acc : tco_instance_var_referrer(n - 1, n * acc)
end
tco_method :tco_instance_var_referrer
end
x = TCOInstance.new
puts x.tco_instance_var_referrer(10000).to_s.length
puts x.tco_cycles
# => 35660
# => 10000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment