如何在Ruby中循环特定块

时间:2016-10-02 18:24:34

标签: ruby

我想循环邮件正文,以便继续发送电子邮件,直到按下 Ctrl + c 。我该怎么做?

Page

1 个答案:

答案 0 :(得分:0)

开始无限循环,你可以选择:

loop do
  # email sending logic here
end

另一种选择是:

while true
  # email sending logic here
end

还有一个是:

until false
  # email sending logic here
end

你明白了。任何这些只会在你停止时停止。

 loop do
  Mail.deliver do
    to 'mikel@test.lindsaar.net'
    from 'ada@test.lindsaar.net'
    subject 'testing sendmail'
    body 'testing sendmail' #how to loop the body to keep sending email
  end
end

修改

在这里的评论中讨论了你如何将body作为论据传递:

def send_mail(body) # pass an argument called `body`
  Mail.deliver do
    to      'mikel@test.lindsaar.net'
    from    'ada@test.lindsaar.net'
    subject 'testing sendmail'
    body    body # use passed into the method argument
  end
end

用法:

send_email('this is a demo body')