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'
end
greeter
end
The built in ruby methods work fine. Is there something wrong with what
I'm doing?
You should put "greeter" outside the class TEST. You cannot call a method
inside a class.
Regards,
Yuanhang.
···
On Thu, Jan 16, 2014 at 10:19 AM, Slipper Spe <lists@ruby-forum.com> wrote:
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'
end
greeter
end
The built in ruby methods work fine. Is there something wrong with what
I'm doing?
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'
end
def self.method_defined_on_Test
puts "Hello, from Test.method_defined_on_Test"
end
# Here, self is the Test class itself
# The three bellow are the same
Test.method_defined_on_Test
self.method_defined_on_Test
method_defined_on_Test
# Here on the Test instance
test = Test.new
test.greeter
puts "You can do anything here, although not recommended!"
end
Abinoam Jr.
···
On Wed, Jan 15, 2014 at 11:56 PM, yuanhang zheng <zhengyhn@gmail.com> wrote:
Hi Slipper Spe,
You should put "greeter" outside the class TEST. You cannot call a method
inside a class.
Regards,
Yuanhang.
On Thu, Jan 16, 2014 at 10:19 AM, Slipper Spe <lists@ruby-forum.com> wrote:
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'
end
greeter
end
The built in ruby methods work fine. Is there something wrong with what
I'm doing?
On Jan 16, 2014 6:08 PM, "Abinoam Jr." <abinoam@gmail.com> wrote:
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'
end
def self.method_defined_on_Test
puts "Hello, from Test.method_defined_on_Test"
end
# Here, self is the Test class itself
# The three bellow are the same
Test.method_defined_on_Test
self.method_defined_on_Test
method_defined_on_Test
# Here on the Test instance
test = Test.new
test.greeter
puts "You can do anything here, although not recommended!"
end
Abinoam Jr.
On Wed, Jan 15, 2014 at 11:56 PM, yuanhang zheng <zhengyhn@gmail.com> > wrote:
> Hi Slipper Spe,
>
> You should put "greeter" outside the class TEST. You cannot call a method
> inside a class.
>
> Regards,
> Yuanhang.
>
>
> On Thu, Jan 16, 2014 at 10:19 AM, Slipper Spe <lists@ruby-forum.com> > wrote:
>>
>> 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'
>> end
>> greeter
>>
>> end
>>
>>
>> The built in ruby methods work fine. Is there something wrong with what
>> I'm doing?
>>
>> --
>> Posted via http://www.ruby-forum.com/\.
>
>