如何将 class 变量获取到模块中
How to get class variable into module
我无法用 class 变量覆盖模块变量。
module Main
class Traks
@@endpoint ='/trakings'
class << self
include ViewTrait
end
end
end
我的特质模块
module Main
module ViewTrait
def view(id:, options: "")
Response::new("#{@@endpoint}/#{id}#{options}").resource(id: id).get
end
end
end
在 ViewTrait 中我无法访问我已经在我的 Traks class 中定义的 @@endponint
。任何人都可以告诉我我做错了什么。
在 ViewTrait 中,我替换了 @@endpoint
,其中 self.class_variable_get(:@@endpoint)
并且我可以访问模块中的 class 变量。希望这会对其他人有所帮助。
我无法用 class 变量覆盖模块变量。
module Main
class Traks
@@endpoint ='/trakings'
class << self
include ViewTrait
end
end
end
我的特质模块
module Main
module ViewTrait
def view(id:, options: "")
Response::new("#{@@endpoint}/#{id}#{options}").resource(id: id).get
end
end
end
在 ViewTrait 中我无法访问我已经在我的 Traks class 中定义的 @@endponint
。任何人都可以告诉我我做错了什么。
在 ViewTrait 中,我替换了 @@endpoint
,其中 self.class_variable_get(:@@endpoint)
并且我可以访问模块中的 class 变量。希望这会对其他人有所帮助。