Created
March 21, 2015 16:59
-
-
Save tdg5/c6a39168b3ac252b812b to your computer and use it in GitHub Desktop.
Instance variable reference from tail recursive call
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
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