4 Replies - 1312 Views - Last Post: 13 September 2011 - 01:09 PM Rate Topic: -----

#1 lnc12  Icon User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 268
  • Joined: 21-May 08

Getting an error: uninitialized constant Dog (NameError)

Posted 13 September 2011 - 04:42 AM

I get this error: uninitialized constant Dog (NameError)
But I have no idea what this error means, heir is mys code:
if __FILE__ == $0
  # TODO Generated stub
  
  dog = Dog.new
  
  dog.name = "Fido"
  dog.age = 11
  dog.gender = "Male"
  dog.color = "Black"
  dog.bark;
  
end




My Dog class
class Dog < Pet
  
  def bark
    puts "w00f"
  end
end



My Pet class
class Pet
  attr_accessor :name, :age, :gender, :color
end



Is This A Good Question/Topic? 0
  • +

Replies To: Getting an error: uninitialized constant Dog (NameError)

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2101
  • View blog
  • Posts: 3,204
  • Joined: 21-June 11

Re: Getting an error: uninitialized constant Dog (NameError)

Posted 13 September 2011 - 04:55 AM

If those code snippets are from the different files, you need to require the file containing Dog from your main file and you need to require the file containing Pet in the file which defines Dog.

If it's all in the same file, you need to move the definition of Pet before the definition of Dog and the definition of Dog before the if __FILE__ == $0 block.
Was This Post Helpful? 0
  • +
  • -

#3 lnc12  Icon User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 268
  • Joined: 21-May 08

Re: Getting an error: uninitialized constant Dog (NameError)

Posted 13 September 2011 - 05:28 AM

They are in diffrent files and I am brand new to ruby so do you mind make a code snip on how to do?
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2101
  • View blog
  • Posts: 3,204
  • Joined: 21-June 11

Re: Getting an error: uninitialized constant Dog (NameError)

Posted 13 September 2011 - 05:32 AM

At the beginning of the main file, add a line which says require "filename" where you replace filename with the name of the file containing your dog class.

Similarly at the beginning of your file defining Dog, add the same line but this time with the name of the file containing the definition of Pet.
Was This Post Helpful? 0
  • +
  • -

#5 lnc12  Icon User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 268
  • Joined: 21-May 08

Re: Getting an error: uninitialized constant Dog (NameError)

Posted 13 September 2011 - 01:09 PM

Eeh I was a bid to fast, but do you mean like this
if __FILE__ == $0
require Dog.rb
  # TODO Generated stub
  
  dog = Dog.new
  
  dog.name = "Fido"
  dog.age = 11
  dog.gender = "Male"
  dog.color = "Black"
  dog.bark;
  
end




This post has been edited by lnc12: 14 September 2011 - 02:41 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1