gpt4 book ai didi

Ruby 模板模块

转载 作者:数据小太阳 更新时间:2023-10-29 08:42:08 25 4
gpt4 key购买 nike

假设我有一系列相关模块:

module Has_Chocolate
def has_chocolate?
true
end
end

module Has_Cake
def has_cake?
true
end
end

. . .

我将如何构建模板模块 Has_Something,其中 Something 将作为模块的参数?

最佳答案

模块在其封装上下文中是常量,对于顶层是内核。这让我们可以使用 const_get 获取模块。试试这个:

module Has_Something
def has(*items)
items.each do |item|
mod = Kernel.const_get("Has_" + item.to_s.capitalize)
instance_eval { include mod }
end
end
end

class Baker
extend Has_Something
has :cake
end

class CandyMan
extend Has_Something
has :chocolate
end

class ChocolateCake
extend Has_Something
has :cake, :chocolate
end

如果您更喜欢 include 而不是 extend,您也可以这样做:

module Has_Something
def self.included(base)
base.extend HasTemplate
end

module HasTemplate
def has(*items)
items.each do |item|
mod = Kernel.const_get("Has_" + item.to_s.capitalize)
instance_eval { include mod }
end
end
end
end

class Baker
include Has_Something
has :cake
end

class CandyMan
include Has_Something
has :chocolate
end

class ChocolateCake
include Has_Something
has :cake, :chocolate
end

无论哪种情况,此代码都是相同的:

steve = Baker.new
bob = CandyMan.new
delicious = ChocolateCake.new
steve.has_cake? && bob.has_chocolate? # => true
delicious.has_cake? && delicious.has_chocolate? #=> true

编辑:

根据您的评论,您正在寻找一种自动创建格式为 has_something? 的方法的方法。这更容易做到:

module Has_Something
def has (*items)
items.each do |item|
method_name = ('has_' + item.to_s + '?').to_sym
send :define_method, method_name do
true
end
end
end
end

class Baker
extend Has_Something
has :cake
end

class CandyMan
extend Has_Something
has :chocolate
end

class ChocolateCake
extend Has_Something
has :cake, :chocolate
end

steve = Baker.new
bob = CandyMan.new
delicious = ChocolateCake.new

steve.has_cake? && bob.has_chocolate? # => true
delicious.has_cake? && delicious.has_chocolate? # => true

关于Ruby 模板模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1153536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com