S4M
S4M

Reputation: 4661

NameError in Ruby

For this piece of code:

class myBaseClass
  def funcTest()
    puts "baseClass"
  end
end
myBaseClass.new.funcTest

I am getting an error:

NameError: undefined local variable or method `myBaseClass' for main:Object
from c:/Users/Yurt/Documents/ruby/polymorphismTest.rb:9
from (irb):145:in `eval'
from (irb):145
from c:/Ruby192/bin/irb:12:in `<main>'
irb(main):152:0> x=myBaseClass.new

When I tryx=myBaseClass.new, I get:

NameError: undefined local variable or method `myBaseClass' for main:Object from (irb):152

Has someone already encountered this problem? I don't think my code can be wrong.

Upvotes: 29

Views: 38164

Answers (4)

john mwadime
john mwadime

Reputation: 1

Code working

class MyBaseClass
  def funcTest()
   puts "baseClass"
 end
end
MyBaseClass.new.funcTest

Upvotes: 0

Steve
Steve

Reputation: 21499

Your class name should start with a capital, working code below

class MyBaseClass
  def funcTest()
   puts "baseClass"
 end
end



MyBaseClass.new.funcTest

Upvotes: 6

Bruno Roh&#233;e
Bruno Roh&#233;e

Reputation: 3534

Your code is wrong. Classnames must start with an uppercase in Ruby.

class MyBaseClass

fixes it.

What I don't get is how you don't get a clear error message like I do.

Upvotes: 3

edgerunner
edgerunner

Reputation: 14973

In ruby, all constants including class names must begin with a capital letter. myBaseClass would be interpreted as an undefined local variable. MyBaseClass would work properly.

Upvotes: 64

Related Questions