-1

以下のルビークラスでレッスンに従おうとしていますが、プレーヤー関数とそれに続く新しい変数を呼び出すことで、出力ステートメントの結果が「John Smith」になる方法がわかりませんか?

これにはもっと簡単な方法がありますか?最後に、TextMate で Ruby クラスまたは 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 = []

また、Rubyデバッグ Gem といくつかのレッスンについては、Ruby Debugger をマスターするをお読みください。

于 2012-07-28T03:36:48.863 に答える