-1

我正在尝试使用下面的 ruby​​ 类学习课程,我只是不明白输出语句的结果是如何通过调用 player 函数后跟新变量的“John Smith”?

有没有更简单的方法呢?编码器以我感到困惑的方式做到了最后,你能告诉我如何调试 Ruby 类或 TextMate 上的任何 ruby​​ 代码吗?我的意思是调试,就像在 Visual C++ 中调试一样,向我展示了第一行被调用和执行的内容,然后跳转到下一行等……看看它是如何工作的?

class Dungun
  attr_accessor :player 

def initialize(player_name)
  @player = Player.new(player_name)
  @rooms = []
end


class Player
  attr_accessor :name, :location
  def initialize(player_name)
    @name = player_name
  end
end

class Room
  attr_accessor :reference, :name, :description, :connection
  def initialize(reference,name,description,connection)
    @reference = reference
    @name = name
    @description = description
    @connection = connection

  end
end
end

my_dungun = Dungun.new("John Smith")
puts my_dungun.player.name
4

1 回答 1

3

执行顺序

# 1. Called from my_dungun = Dungun.new("John Smith")
Dungun.new("John Smith")

# 2. Inside Dungun it will call the initialize from Dungun class
initialize("John Smith")

# 3. The initialize method, from Dungun class, will have this statement saying
# that instance variable @player will receive the
# result of Player.new("John Smith")
@player = Player.new("John Smith")

# 4. The Player's 'new' method will call the
# inner class Player's initialize method
initialize("John Smith")

# 5. The Player's initialize should assign "Jonh Smith" to @player's name
@name = "John Smith"

# 6. Then head back to where we stopped, and continue to the other
# statement at second line inside Dungun's 'new' method
@rooms = []

并阅读Mastering the Ruby Debugger以获得 ruby​​ 调试 gem 和一些课程!

于 2012-07-28T03:36:48.863 回答