Confused about NoMethodError in Ruby

Posted by E L on Stack Overflow See other posts from Stack Overflow or by E L
Published on 2012-11-26T22:55:00Z Indexed on 2012/11/26 23:03 UTC
Read the original article Hit count: 222

Filed under:

In a simple Ruby example, I'm getting an error that does not occur in irb.

name = "Joe"
def say_hi
  "\"Hi there!\" said #{self}"
end
response = name.say_hi
puts response

This code should return, "Hi there!" said Joe.

It works perfectly fine in irb. However, when I attempt to put the same code in a file and run the file, I get this error:

say_hi.rb:8:in `<main>': private method `say_hi' called for "Joe":String (NoMethodError)

Any suggestion about why this happens?

© Stack Overflow or respective owner

Related posts about ruby