简单的Ruby类混淆与调试
我正试图学习下面的ruby类,我只是不明白在调用新变量后的player函数时,输出语句的结果如何是“John Smith” 有没有更简单的方法?编码员的方式让我感到困惑 最后,您能告诉我如何在TextMate上调试Ruby类或任何Ruby代码吗?我的意思是调试就像调试VisualC++中显示的第一行得到调用和激励,然后跳到下一个…看看它是如何工作的简单的Ruby类混淆与调试,ruby,ruby-on-rails-3,Ruby,Ruby On Rails 3,我正试图学习下面的ruby类,我只是不明白在调用新变量后的player函数时,输出语句的结果如何是“John Smith” 有没有更简单的方法?编码员的方式让我感到困惑 最后,您能告诉我如何在TextMate上调试Ruby类或任何Ruby代码吗?我的意思是调试就像调试VisualC++中显示的第一行得到调用和激励,然后跳到下一个…看看它是如何工作的 class Dungun attr_accessor :player def initialize(player_name) @play
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
执行顺序
# 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 = []
阅读ruby调试宝石和一些课程 你的代码也很混乱,为什么我要使用@player=player.new在这里面,会有更多的玩家需要添加。这不是代码。这是调用语句的顺序。我更新了我的答案,试图让它更适合您@user1556385