对于未来的观众,我们将这种方法用于非常相似的事情:
module HasBulkReplies
extend ActiveSupport::Concern
module ClassMethods
# Defines responses to multiple messages
# e.g. reply_with false, to: :present?
# reply_with "", to: %i(to_s name description)
# reply_with true, to: %i(empty? nothing? void? nix? nada? meaning?)
def reply_with(with, to:)
[to].flatten.each do |message|
define_method message do
with
end
end
end
end
end
这定义了一个 DSL,它允许我们:
class Work
class Codes
class NilObject
include HasBulkReplies
include Singleton
reply_with true,
to: :nil?
reply_with false,
to: %i(
archaeology?
childrens?
educational_purpose?
geographical?
historical?
interest_age?