나는 Hardways를 통해 작업 중이며 각 룸이 자체 클래스이고 경로가되는 엔진 클래스가있는 텍스트 모험을하는 것을 연습합니다. 한 반에서 다른 반으로. 또한 여러 개의 파일이 있어야합니다.클래스가 다른 클래스를 호출하는 엔진에 데이터를 보내도록하십시오.
현재 사용중인 코드를 사용하면 클래스 또는 메서드 외부에서 엔진을 사용할 수 있지만 세 번째 클래스에서 Engine 클래스를 호출하면 팔콘 (클래스 이름)이 unitialized라는 메시지가 나타납니다.
저는 스타 워즈에서 게임을 기반으로하고 있으며, 다른 방법으로 문제에 접근하고 있음에도 불구하고 제공 할 수있는 도움에 대해 대단히 감사합니다.
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