Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NameError in Ruby

Tags:

class

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.

like image 922
S4M Avatar asked Jun 26 '11 20:06

S4M


People also ask

What does uninitialized constant mean in Ruby?

Ruby NameError Uninitialized Constant Causes You'll see this error when the code refers to a class or module that it can't find, often because the code doesn't include require, which instructs the Ruby file to load the class.

What is the difference between a class and a module Ruby?

What is the difference between a class and a module? Modules are collections of methods and constants. They cannot generate instances. Classes may generate instances (objects), and have per-instance state (instance variables).


1 Answers

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.

like image 137
edgerunner Avatar answered Sep 20 '22 13:09

edgerunner