Skip to content
Poignant Guide Ruby Q&A

What does undefined local variable or method mean in Ruby?

Asked by Pablo M. · Errors and debugging help2 repliesupdated

Short answer

ok so im doing the little greeting program from my school Ruby project and it crashes and i dont get the error. the variable is RIGHT THERE

Written by Pablo M. ·
CitePoignant Guide Ruby Q&A, "What does undefined local variable or method mean in Ruby?", https://poignantguide.net/answers/read/what-does-undefined-local-variable-or-method-mean-in-ruby/, accessed 2026-12-04.
  1. Member ·
    def greet
      puts "Hi #{name}"
    end
    
    name = "Pablo"
    greet
    
    greeter.rb:2:in `greet': undefined local variable or method `name' for main:Object (NameError)
    

    Ruby 3.3, running it with ruby greeter.rb. I typed name correctly, I checked 3 times. I also tried moving name = "Pablo" above the def and it still crashes. chatgpt said it was a "scope issue" but didnt really explain what that means and i dont want to just copy the fix.

    what does "undefined local variable or method" mean, is it a variable or a method?? and how do i find out what went wrong when i see this error next time

    0
    ReplyLink

Plain text or simple Markdown. Links are nofollow. Your email is never shown.

  1. Junko T.Member ·

    The message says "variable or method" because Ruby really does not know which one you meant. A bare word like name could be a local variable or a call to a method named name, and Ruby looked for both and found neither. That ambiguity is in the wording on purpose.

  2. Kai N.Member ·

    Make it a global: $name = "Pablo" and use $name inside greet. Works everywhere, and it is the fastest way to get the program running before the deadline. I do it in all my small scripts.

Poignant Guide Ruby Q&A. Questions and answers on learning Ruby and programming: syntax, blocks, objects, gems, Rails, debugging and reading the Poignant Guide.