整个执行过程中这些Ruby实例变量的值是什么?

时间:2014-02-18 23:56:25

标签: ruby

我正在尝试在程序执行时跟踪实例变量@x@y@z的值:

class S    
  def initialize(theX, theY)
    @x = theX + 2
    @y = theY + 3
  end

  def f(n)
    @x = n * @x + @y
    @y = @x + (n + 1) * @y
  end

  def to_s
    return (@y + 1).to_s + " " + (@x + 2).to_s  
  end
end

class T < S
  def initialize
    super(1, 4)
    @z = @x + @y + 3
  end

  def f(m)
    super(3)
    @z *= m   
  end

  def to_s
    return super + " " + (@z + 3).to_s
  end
end

s = S.new(6, 5)
s.f(3)
print s, "\n"
t = T.new
t.f(7)
print t, "\n"

最终输出是:

65 34
45 18 94

任何人都可以逐步说明程序执行如何产生这些值吗?

1 个答案:

答案 0 :(得分:2)

尝试按照下面的图片或在How to track the execution process of ruby program

查看@ ArupRakshit的回答

enter image description here