Search code examples
rubynomethoderrormuttmethod-missing

ruby: method_missing backtick typo?


I was building a method to send me an email through mutt when my ruby scripts fail. It looks something like this:

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

the command was throwing the following error:

sh: -c: line 1: unexpected EOF while looking for matching ``'
sh: -c: line 2: syntax error: unexpected end of file

I finally looked closely enough to see that $! contains a backtick before the undefined method's name followed by a single quote:

undefined method `UnknownFunction' for main:Object

I dug into the code and verified the method_missing method has a backtick before and single quote afterwards. Should the backtick be a single quote or vice versa? If not, what's the reasoning behind it?

raise NoMethodError, "undefined method `#{mid}' for #{self}", caller(1)

Solution

  • It's a substitute for an open single quote (‘) in plain text/pre-Unicode environments. See: Why do plain-text technical articles often enclose terms within backticks and single quotes?