我正在通過Rails the Hardway進行工作,並準備練習45,其中涉及到每個房間都是自己的類,並且有一個引擎類路由你從一個班級到另一個班級。另外,必須有幾個文件。讓班級發送數據到一個調用另一個班級的引擎
我目前使用的代碼將允許我在類或方法之外使用引擎,但如果我從第三個類調用引擎類,我收到一條消息,稱Falcon(類名)是單元化的。
我以星球大戰爲基礎進行遊戲,非常感謝您提供的任何幫助 - 即使這意味着以不同的方式接近問題。
runner.rb:
module Motor
def self.runner(class_to_use, method_to_use = nil)
if method_to_use.nil? == false
room = Object.const_get(class_to_use).new
next_room.method(method_to_use).call()
else
room = Object.const_get(class_to_use).new
puts room
end
end
end
map.rb require_relative '轉輪' require_relative '字符'
class Falcon
def luke
puts "It works!"
end
def obi_wan
puts "this is just a test"
end
end
characters.rb
class Characters
include Motor
puts "You can play as Luke Skywalker or Obi-wan Kenobi"
puts "Which would you like?"
character = gets.chomp()
if character == "Luke Skywalker"
puts "The Force is strong with this one."
Motor.runner(:Falcon, :luke)
elsif character == "Obi Wan Kenobi"
puts "It's been a long time old man."
Motor.runner(:Falcon, :obi_wan)
else
puts "I have no idea what you're saying."
end
end