I am creating a gem to support some command line mailings. I use some gem. I am using Mail Gem . As you can see in the mail gem description, this is something like this.
mail = Mail.new do from ' mikel@test.lindsaar.net ' to ' you@test.lindsaar.net ' subject 'This is a test email' body File.read('body.txt') end
In the block, I call methods from the Mail class (from, to, subject, body). That makes sense, so I create it in my mail program class
def initialize(mail_settings, working_hours) @mail_settings = mail_settings @working_hours = working_hours @mailer = Mail.new do to mail_settings[:to] from mail_settings[:from] subject mail_settings[:subject] body "Start #{working_hours[:start]} \n\ Ende #{working_hours[:end]}\n\ Pause #{working_hours[:pause]}" end end
It looks straight. Just call the block and fill in my values ββthat I get through the constructor. Now my question comes.
I tried to put the body structure for mail in a separate method. But I cannot use it in the Mail constructor for a gem.
module BossMailer class Mailer def initialize(mail_settings, working_hours) @mail_settings = mail_settings @working_hours = working_hours @mailer = Mail.new do to mail_settings[:to] from mail_settings[:from] subject mail_settings[:subject] body mail_body end end def mail @mailer.delivery_method :smtp, address: "localhost", port: 1025 @mailer.deliver end def mail_body "Start
end
This error exited this code. 
This means that I cannot use my class method or class variable (starting with @a ) in this block.
Questions
What is the execution order in the block? If I set my @mail_settings variable, I cannot use it in a block. Is Ruby to search @mail_settings in the Mail class where I give the block? Why can I use this parameter from the BossMailer::Mailer constructor through the block and an error does not appear?
And why does this work if I use a variable to parse the contents in the block? ( body_content = mail_body ) works!
def initialize(mail_settings, working_hours) @mail_settings = mail_settings @working_hours = working_hours body_content = mail_body @mailer = Mail.new do to mail_settings[:to] from mail_settings[:from] subject mail_settings[:subject] body body_content end end