NameError in Ruby

34,586

Solution 1

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.

Solution 2

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

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



MyBaseClass.new.funcTest

Solution 3

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.

Share:
34,586
S4M
Author by

S4M

Updated on August 17, 2022

Comments

  • S4M
    S4M almost 2 years

    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.