我正在使用SendGrid模块(require 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
)返回邮件对象设置并准备就绪。我的问题是,您是否需要模块中的模块(在我的自定义模块中需要sendgrid-ruby )。
class Thing
include MyModule
def doMail
mail = Thing.standardMail
end
end
答案 0 :(得分:1)
我不确定为什么你需要一个模块,在这种情况下扩展Sendgrid的默认行为要容易得多:
class MyMailer < SendGrid::Mail
def initialize(params)
@to = 'js@lso.com'
@from = 'no-reply@gsdfdo.com'
@subject = 'Boo'
@html = " "
@text = " "
super
end
end
或者您可以直接覆盖:
class SendGrid::Mail
def initialize(params)
@to = 'js@lso.com'
@from = 'no-reply@gsdfdo.com'
@subject = 'Boo'
@html = " "
@text = " "
super
end
end
答案 1 :(得分:-1)
之间没有区别:
module Foo
require 'bar'
# ...
end
和
require 'bar'
module Foo
# ...
end
所以,是的,您可以在模块中要求 module Ruby文件,但没有理由这样做。