私はRailstheHardwayで作業しており、演習45まで進んでいます。この演習では、各部屋が独自のクラスであり、あるクラスから別のクラスにルーティングするエンジンクラスがあります。さらに、いくつかのファイルが必要です。
現在使用しているコードを使用すると、クラスまたはメソッドの外部でエンジンを使用できますが、3番目のクラスからEngineクラスを呼び出すと、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.rbrequire_relative'ランナー'require_relative'文字'
class Falcon
def luke
puts "It works!"
end
def obi_wan
puts "this is just a test"
end
end
文字.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