Ruby - 使用 Mixins 获取在模块中计算的父变量

Ruby - Using Mixins to get a Parent Variable calculated in a Module

非常感谢您的帮助。 我在两个单独的文件中有一个父项、一个子项和一个模块。 player_fabrizioPlayer class 的实例,它是 Settings 的子项。实例player_fabrizio需要使用实例new_settings@totalmoney变量。

require_relative "modules_test"

class Settings
    include Variables
    attr_reader :totalmoney
end

class Player < Settings
    def initialize(kingdom, king)
        @playerData = [kingdom, king]
    end
    def calculate
        print @totalmoney
    end
end

new_settings = Settings.new
new_settings.globalSettings(100, 2)

player_fabrizio = Player.new("MyDinasty", "Fabrizio")
player_fabrizio.calculate                # no output

这是文件 modules_test.rb。 new_setting使用module Variables的方法globalSettings来设置@totalmoney变量。

module Variables
    def globalSettings(totalmoney, nplayer)
        @totalmoney = totalmoney
        return @totalmoney
    end
end

方法player_fabrizio.calculate的结果应该是100,但是没有输出。 我搜索了论坛,但找不到问题。我读了一个有类似问题的问题,但找不到答案。

非常感谢你的帮助 法布里齐奥

(Ruby, mixin instance variables and methods)

我会将 Settings 重命名为 Game 并按如下方式进行(尽管您可以自由使用适合您的任何名称):

class Game
  include Variables
  attr_reader :totalmoney
end

class Player
  attr_reader :game

  def initialize(game, kingdom, king)
    @game = game
    @playerData = [kingdom, king]
  end

  def calculate
    print game.totalmoney
    # rest of the logic here
  end
end

game = Game.new
game.globalSettings(100, 2)

player_fabrizio = Player.new(game, "MyDinasty", "Fabrizio")
player_fabrizio.calculate

如果有帮助请告诉我。

简答 - 您正在设置 @totalmoney,它是 new_settingsinstance 成员变量,因此 player_fabrizio 无权访问给它。你可能想设置一个 class 的成员变量 Settings.

更详细地说-模块在这里有点小问题。通过将 Variables 包含到 Settings 中,您实际上是将 Variables 的代码粘贴到 Settings 中。所以你所拥有的相当于这个:

class Settings        
    attr_reader :totalmoney

    def globalSettings(totalmoney, nplayer)
        @totalmoney = totalmoney
        return @totalmoney
    end
end

class Player < Settings
    def initialize(kingdom, king)
        @playerData = [kingdom, king]
    end
    def calculate
        print @totalmoney
    end
end

new_settings = Settings.new
new_settings.globalSettings(100, 2)

player_fabrizio = Player.new("MyDinasty", "Fabrizio")
player_fabrizio.calculate                # no output

到最后,您有一个名为 new_settingsSettings 对象,其中 @totalmoney 设置为 100;并且您有一个名为 player_fabrizioPlayer 对象(也是一个 Settings),它根本没有设置 @totalmoney

您可能想要的是:

class Settings        
    def self.globalSettings(totalmoney, nplayer)
        @@totalmoney = totalmoney
    end
end

class Player < Settings
    def initialize(kingdom, king)
        @playerData = [kingdom, king]
    end
    def calculate
        print @@totalmoney
    end
end

Settings.globalSettings(100, 2)

player_fabrizio = Player.new("MyDinasty", "Fabrizio")
player_fabrizio.calculate

注意 @@totalmoney 前面的 @@,表示它是 class 成员。我还制作了 globalsettings 一个 class 方法,因此您不需要手头的 Settings 对象来更改全局设置。

不过老实说,我对从 Settings 继承 Player 有点怀疑 - 播放器不是一种特殊类型的设置!就个人而言,我可能会做更多类似的事情:

class Settings
    def self.globalSettings(totalmoney, nplayer)
        @@totalmoney = totalmoney
    end

    def self.totalmoney
      @@totalmoney
    end
end

class Player
    def initialize(kingdom, king)
        @playerData = [kingdom, king]
    end
    def calculate
        print Settings.totalmoney
    end
end

Settings.globalSettings(100, 2)

player_fabrizio = Player.new("MyDinasty", "Fabrizio")
player_fabrizio.calculate