Undefined Local Variable or Method

Today, I started to try to learn ruby. When I went to make my first
method, I kept receiving an error “undefined local variable or method.”

I tried looking up how to make methods in Ruby online, so that I would
be sure its not my own fault. I copy pasted several examples into my
ide, all of which returned the same error.

I made a simple method (below) and I get the same error: class:TEST’:
undefined local variable or method `greeter’ for TEST:Class (NameError)

class TEST

def greeter()
puts ‘Hi’


The built in ruby methods work fine. Is there something wrong with what
I’m doing?

Hi Slipper Spe,

You should put “greeter” outside the class TEST. You cannot call a
inside a class.


Ahhh, I see. Earlier, I tried putting it outside, but below the class.
Putting it above works.

Thank you good sir, you helped me a lot :slight_smile:

Wow, this amazes me. Thank you, Abinoam Jr.

Yuanhang Zheng.

Hi Yuanhang Zheng,

That’s right, but I would add that “You cannot call THIS method (the
way you’re doing)”.

But, the code inside the class has not such limitations in Ruby.
Just don’t abuse it, or your code will be a mess!

class Test

def greeter()

Here, self is an instance of the Test class

puts 'Hi, from Test#greeter'


def self.method_defined_on_Test
puts “Hello, from Test.method_defined_on_Test”

Here, self is the Test class itself

The three bellow are the same


Here on the Test instance

test = Test.new

puts “You can do anything here, although not recommended!”


Abinoam Jr.