Search code examples
rubymacossublimetext

Why do I recieve an 'undefined local variable or method' message for any string i write on Mac's TextEdit?


I'm trying to learn to code, starting with ruby. While using TextEdit I was able to run numerical code, but any variation of puts "text" returned the error message of

undefined local variable or method '"text"' for main:Object (NameError).

I used another text editor and it worked just fine. Can anyone help me understand what went wrong? Thank you!

I'm now using Sublime Text instead of TextEdit.


Solution

  • A common source of these errors is using an editor which replaces the "plain" quote characters " ' with typographic quotes. Your original code

    puts "text"
    

    may thus be replaced by your editor with

    puts “text”
    

    Ruby always requires the "plain" quoting characters with string literals however. When using other characters, Ruby assumes that you want to call a method called “text” and fails accordingly as you didn't define such a method.

    In the end, it is usually a good idea to use an editor which is geared for code editing (such as Sublime, VisualStudio Code, Textmate, Nova, or others) rather than an editor geared towards text editing (such as TextEdit, Pages, Word, ...), specifically to avoid such issues.