我正在通过 Rails the Hardway 进行练习,并且要进行 45 次练习,其中涉及进行文本冒险,其中每个房间都是它自己的课程,并且有一个引擎课程可以将您从一个课程引导到另一个课程。此外,必须有多个文件。
我当前使用的代码将允许我在类或方法之外使用引擎,但如果我从第三个类调用 Engine 类,我会收到一条消息,指出 Falcon(类名)已单元化。
我的游戏基于《星球大战》,非常感谢你能提供的任何帮助——即使这意味着以不同的方式解决问题。
亚军.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
字符.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