在Ruby中使用块的顺序是什么

时间:2015-07-27 10:38:13

标签: ruby block

我正在创建一个gem来支持命令行中的一些Mailing。我用了一些宝石。 我正在使用Mail Gem。正如您在mail gem的描述中所看到的那样。

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

在块中,我调用Mail类中的方法(from,to,subject,body)。这是有道理的,所以我在我自己的邮件程序类

中构建它
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

这看起来很直接。只需调用块并填写我通过构造函数获取的值。 现在来了我的问题。

我试图将邮件的主体结构放入一个单独的方法中。但是我不能在gem的Mail构造函数中使用它。

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 #{working_hours[:start]} \n\
    Ende #{working_hours[:end]}\n\
    Pause #{working_hours[:pause]}"
  end
end

此错误出现在此代码中。 enter image description here

这意味着我不能在这个块中使用我的类方法或类变量(以@a开头)。

问题

Block中执行的顺序是什么?如果我设置我的变量@mail_settings,我就不能在块中使用它。 Ruby在@mail_settings类中搜索Mail我在哪里给出块?为什么我可以通过块使用BossMailer::Mailer构造函数中的给定参数,并且不会出现错误?

如果我使用和变量将内容解析为块,为什么这会起作用? (body_content = mail_body)有效!

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

2 个答案:

答案 0 :(得分:3)

这完全取决于背景。

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

fromto方法(以及其他方法)是Mail::Message实例上的方法。为了能够以这种漂亮的DSL方式调用它们,传递给构造函数的块是instance_eval'ed

这意味着在这个块的内部,self不再是您的邮件程序,而是邮件消息。因此,您的邮件程序方法无法访问。

而不是instance_eval,它们可能只有yieldblock.call,但这不会使DSL成为可能。

至于为什么局部变量有效:这是因为ruby块是词法范围的闭包(意思是,它们保留了它们声明的局部上下文。如果有一个局部变量可见块的位置已定义,它将记住调用块时的变量及其值)

替代方法

请勿使用阻止形式。使用此:https://github.com/mikel/mail/blob/0f9393bb3ef1344aa76d6dac28db3a4934c65087/lib/mail/message.rb#L92-L96

mail = Mail.new
mail['from'] = 'mikel@test.lindsaar.net'
mail[:to]    = 'you@test.lindsaar.net'
mail.subject 'This is a test email'
mail.body    = 'This is a body'

代码

尝试评论/取消注释某些行。

class Mail
  def initialize(&block)
    # block.call(self) # breaks DSL
    instance_eval(&block) # disconnects methods of mailer
  end

  def to(email)
    puts "sending to #{email}"
  end

end

class Mailer
  def admin_mail
    # get_recipient = 'vasya@example.com'
    Mail.new do
      to get_recipient
    end
  end

  def get_recipient
    'sergio@example.com'
  end
end


Mailer.new.admin_mail

答案 1 :(得分:2)

问题是mail_body是在Mail::Message的上下文中评估的,而不是在BossMailer::Mailer类的上下文中。请考虑以下示例:

class A
  def initialize
    yield
  end
end

class B
  def initialize(&block)
    instance_eval { block.call }
  end
end

class C
  def initialize(&block)
    instance_eval(&block)
  end
end

class Caller
  def test
    A.new { hi 'a' }
    B.new { hi 'b' }
    C.new { hi 'c' }
  end

  def hi(x)
    puts "hi there, #{x}"
  end
end

Caller.new.test

这会让你

hi there, a
hi there, b
`block in test': undefined method `hi' for #<C:0x286e1c8> (NoMethodError)

查看gem的代码,这正是发生的事情:

Mail.new just passes the block given to Mail::Message's constructor

The said constructor works exactly as the C case above

<小时/> instance_eval基本上会更改当前上下文中self的内容。

关于为什么BC案例的工作方式不同 - 您可以认为&会将block对象从proc更改为块(是的,我选择的变量名称不是很好)。更多关于差异here

相关问题