Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用self.Class Ruby/Rails返回模块类而不是模型类_Ruby On Rails_Ruby_Module_Constants_Self - Fatal编程技术网

Ruby on rails 使用self.Class Ruby/Rails返回模块类而不是模型类

Ruby on rails 使用self.Class Ruby/Rails返回模块类而不是模型类,ruby-on-rails,ruby,module,constants,self,Ruby On Rails,Ruby,Module,Constants,Self,我试图通过实现模块来干燥代码。但是,我有存储在模型(而不是模块)中的常量,我正试图使用self.class访问这些常量 以下是(我希望)相关的片段: module Conversion def constant(name_str) self.class.const_get(name_str.upcase) end end module DarkElixir def dark_elixir(th_level) structure.map { |name_str| st

我试图通过实现模块来干燥代码。但是,我有存储在模型(而不是模块)中的常量,我正试图使用self.class访问这些常量

以下是(我希望)相关的片段:

module Conversion
  def constant(name_str)
    self.class.const_get(name_str.upcase)
  end
end

module DarkElixir
  def dark_elixir(th_level)
    structure.map { |name_str| structure_dark_elixir(name_str, th_level) if constant(name_str)[0][:dark_elixir_cost] }.compact.reduce(:+)
  end
end

class Army < ActiveRecord::Base
  include Conversion, DarkElixir

  TH_LEVEL = [...]
end

def structure_dark_elixir(name_str, th_level)
  name_sym = name_str.to_sym
  Array(0..send(name_sym)).map { |level| constant(name_str)[level][:dark_elixir_cost] }.reduce(:+) * TH_LEVEL[th_level][sym_qty(name)]
end
模块转换
def常量(name_str)
self.class.const\u get(name\u str.upcase)
结束
结束
模块LIXIR
def暗黑长生不老药(th_级别)
structure.map{| name_str | structure_dark_elixir(name_str,th_level)if constant(name_str)[0][:dark_elixir_cost]}.compact.reduce(:+)
结束
结束
类陆军
当我将structure_dark_elixir方法放在darklixir模块中时,我得到一个错误,“uninitialized constant darklixir::TH_LEVEL”

如果我把它放在Army类中,它会找到合适的常量

我相信这是因为我没有确定自我常数的范围。我想在模块中保留该方法,因为其他模型需要运行引用其自身TH_级常量的方法


我怎样才能做到这一点呢?

为什么不直接使用类方法呢

module DarkElixir
  def dark_elixir(th_level)
    # simplified example
    th_level * self.class.my_th_level
  end
end

class Army < ActiveRecord::Base
  include DarkElixir

  def self.my_th_level
    5
  end
end
模块
def暗黑长生不老药(th_级别)
#简化示例
第四级*self.class.my第四级
结束
结束
类陆军
Ugh。所讨论的方法使用两个常量。这是第二个常数,而不是第一个常数。在第二个常量之前添加了“self.class::”——返回业务

def structure_dark_elixir(name_str, th_lvl)
  name_sym = name_str.to_sym
  Array(0..send(name_sym)).map { |level| constant(name_str)[level][:dark_elixir_cost] }.reduce(:+) * self.class::TH_LEVEL[th_lvl][sym_qty(name_str)]
end 

我相信这需要我将self.my_th_level类方法添加到我包含的每个模型DarkLixir模块中。我希望通过该模块将该方法动态添加到所有模型中。但是您说:
其他模型需要运行引用其自身TH_级常量的方法。那么,你是想让每个类的“th_level”(常量或方法,无所谓)都是特定的,还是对包含DarkElixir模块的每个类都是相同的?嗯,我刚才看到了“正在编辑”并删除了我的评论,以避免干扰itEach模型有自己的th_LVL常量——具有唯一的信息。每个模型都需要一个相同的方法来使用其各自的THU LVL常数进行计算。我试图将该方法存储在模块中以避免重复。使用模型调用常量(name_str)函数时,其行为正常。但是,当在DarkLixir模块中调用时,它会在模块中查找常量,而不是相应的模型。是的,我建议您避免混淆
#const_get
的暗魔法。只需使用类方法而不是常量。所以我建议你只使用self.my_th_level
(你喜欢的名字)而不是thu LVL常量。使用该常量的方法将只使用self.my\u-level
(该调用将转到Model类,该类有自己的my\u-level,并为给定的模型值定义了specific)。