Ruby on rails Ruby需要模块中的模块吗?

Ruby on rails Ruby需要模块中的模块吗?,ruby-on-rails,ruby,module,mixins,Ruby On Rails,Ruby,Module,Mixins,我正在使用SendGrid模块(需要SendGrid ruby),但将这样的代码放在任何地方都不是很枯燥 client = SendGrid::Client.new(api_key: SENDGRID_KEY) mail = SendGrid::Mail.new do |m| m.to = 'js@lso.com' m.from = 'no-reply@gsdfdo.com' m.subject = 'Boo' m.ht

我正在使用SendGrid模块(
需要SendGrid ruby
),但将这样的代码放在任何地方都不是很枯燥

client = SendGrid::Client.new(api_key: SENDGRID_KEY)
      mail = SendGrid::Mail.new do |m|
        m.to = 'js@lso.com'
        m.from = 'no-reply@gsdfdo.com'
        m.subject = 'Boo'
        m.html = " "
        m.text = " "
      end
我的想法是创建一个模块
MyModule
,它将创建一个名为standardMail的方法

module MyModule
    require 'sendgrid-ruby'
    def standardMail
          mail = SendGrid::Mail.new do |m|
            m.to = 'js@lso.com'
            m.from = 'no-reply@gsdfdo.com'
            m.subject = 'Boo'
            m.html = " "
            m.text = " "
          end
     return mail
    end 
end
然后我可以使用
standardMail
(通过
include MyModule
)返回邮件对象设置并准备就绪。我的问题是您是否需要模块中的模块(aka require sendgrid ruby in My custom module


以下两者之间没有区别:

module Foo
  require 'bar'
  # ...
end


因此,是的,您可以在模块内要求模块Ruby文件,但没有什么理由这样做。

以下两者之间没有区别:

module Foo
  require 'bar'
  # ...
end


因此,是的,您可以在模块中要求模块Ruby文件,但没有什么理由这样做。

我不确定您为什么需要模块。在这种情况下,扩展Sendgrid的默认行为会容易得多:

class MyMailer < SendGrid::Mail
 def initialize(params)
    @to = 'js@lso.com'
    @from = 'no-reply@gsdfdo.com'
    @subject = 'Boo'
    @html = " "
    @text = " "

    super
  end
end

我不确定您为什么需要一个模块。在这种情况下,扩展Sendgrid的默认行为会容易得多:

class MyMailer < SendGrid::Mail
 def initialize(params)
    @to = 'js@lso.com'
    @from = 'no-reply@gsdfdo.com'
    @subject = 'Boo'
    @html = " "
    @text = " "

    super
  end
end

“模块中包含模块”和“模块中需要模块”是两个截然不同的概念。特别是,
requiremymodule
无效。修复了问题标题“模块中包含模块”和“模块中require模块”是两个截然不同的问题。特别是,
requiremymodule
无效。修复了我对Ruby不熟悉的问题,因此没有想到这一点。但我不希望所有SendGrid::Mail都被覆盖,在我的应用程序中只覆盖30次。还有10种用法我没有使用我的标准to、from和subject。我想使用你的代码,我只会在30种情况下使用MyMailer,其他10种情况下使用官方的SendGrid::Mail。然后使用第一个示例,它扩展了mailer,但没有覆盖其默认功能。我对Ruby是新手,所以没有想到这一点。但我不希望所有SendGrid::Mail都被覆盖,在我的应用程序中只覆盖30次。还有10种用法我没有使用我的标准to、from和subject。我想使用您的代码,我只会在30种情况下使用MyMailer,其他10种情况下只会正常使用官方SendGrid::Mail?然后使用第一个示例,它扩展了mailer,但不会覆盖其默认功能。
class SendGrid::Mail
  def initialize(params)
    @to = 'js@lso.com'
    @from = 'no-reply@gsdfdo.com'
    @subject = 'Boo'
    @html = " "
    @text = " "

    super
  end
end