2015-05-08 43 views
3

紅寶石抱怨我不是爲了我的劇本,這是提供足夠的論據:紅寶石引發ArgumentError當實際提供正確的參數

#!/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 '[email protected]' 
     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,"[email protected]") 

什麼,我得到的回覆是:

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

這可能與Ruby基本方法send相沖突。嘗試重命名sendsend_mail(或其他),以避免覆蓋send方法

+0

謝謝,這確實是問題所在。老實說,我覺得這是一個很不舒服的'功能'。 – JBoy

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被調用只有一個參數而不是兩個。

相關問題