实际提供正确参数时的Ruby ArgumentError

时间:2015-05-08 14:21:01

标签: ruby email

Ruby抱怨我没有为我的脚本提供足够的参数,即:

    #!/usr/bin/ruby
require 'mail'

def send(file,recipients_csv)
     recipients=recipients_csv.split(",")
      recipients.each do |recipient|
       Mail.defaults do
        delivery_method :smtp,{ :address => 'localhost', :port => 25,:openssl_verify_mode => OpenSSL::SSL::VERIFY_NONE}
       end
        mail = Mail.new do
         from 'noreply@mydomain.com'
         to "#{recipient}"
         subject "test"
         body "test"
         add_file :filename => "a_testfile.tsv", :content => File.read(file.path)
        end
       mail.deliver!
      end
end

testfile=File.new("newfile.tsv","w")
send(testfile,"name@mydomain.com")

我得到的是:

Mailer.rb:4:in `send': wrong number of arguments (1 for 2) (ArgumentError)
    from /usr/lib64/ruby/gems/1.9.1/gems/treetop-1.4.15/lib/treetop/runtime/compiled_parser.rb:18:in `parse'
    from /usr/lib64/ruby/gems/1.9.1/gems/mail-2.5.4/lib/mail/elements/address_list.rb:26:in `initialize'
    from /usr/lib64/ruby/gems/1.9.1/gems/mail-2.5.4/lib/mail/fields/common/common_address.rb:9:in `new'

我不明白,我提供的论据显然是2

2 个答案:

答案 0 :(得分:2)

当你在第22行自己运行脚本时,这个错误不是来自你明显传递给它的两个参数。它实际上来自您在错误堆栈中看到的三个文件之一。

from /usr/lib64/ruby/gems/1.9.1/gems/treetop-1.4.15/lib/treetop/runtime/compiled_parser.rb:18:in `parse'
from /usr/lib64/ruby/gems/1.9.1/gems/mail-2.5.4/lib/mail/elements/address_list.rb:26:in `initialize'
from /usr/lib64/ruby/gems/1.9.1/gems/mail-2.5.4/lib/mail/fields/common/common_address.rb:9:in `new'

如果你进入那些文件send只用一个参数而不是两个参数调用。

答案 1 :(得分:2)

这可能与Ruby基础send方法冲突。尝试将send重命名为send_mail(或其他内容),以避免覆盖send方法