当我的 ruby 脚本失败时,我正在构建一种通过 mutt 向我发送电子邮件的方法。它看起来像这样:
begin
UnknownFunction()
rescue
subject = 'Error'
to_array = ['email@email.com','email2@email.com']
body = "An error occurred:\n#{$!}"
%x[echo "#{body}" | mutt -s "#{subject}" #{to_array.join(",")}]
end
该命令引发以下错误:
sh: -c: line 1: unexpected EOF while looking for matching ``'
sh: -c: line 2: syntax error: unexpected end of file
我终于仔细看了一下,看到了$!在未定义的方法名称前包含一个反引号,后跟一个单引号:
undefined method `UnknownFunction' for main:Object
我深入研究了代码并验证了 method_missing 方法之前有一个反引号,之后有一个单引号。反引号应该是单引号还是反之亦然?如果不是,其背后的原因是什么?
raise NoMethodError, "undefined method `#{mid}' for #{self}", caller(1)