We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,278 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Help a Noobie Please...

I'm getting a mismatch error and I've looked up why it would appear and I'm confused because what I'm doing doesn't match the error cause. Plus I've followed the instructions from a guide that I'm using.
The code on irb...

>> class Cat
>> attr_accessor :name, :age, :gender
>> end
=> nil
>> class Pets
>> attr_accessor :name, :age, :gender
>> end
=> nil
>> class Cat < Pets
>> end
TypeError: superclass mismatch for class Cat
from (irb):24

any thoughts? thanks.

2
Contributors
3
Replies
7 Hours
Discussion Span
1 Year Ago
Last Updated
4
Views
Tupinambiss
Newbie Poster
2 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Perhaps you meant something like

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

class Cat < Pet
end

where Cat is a derivative of Pet ?

What you have doesnt make very much sense. you create two distinct classes and then try to create another class (of the same name) and have a relationship between them. You need to associate the super/sub class relationship when you create the class the first time otherwise you get this ambiguity you are seeing.

L7Sqr
Practically a Posting Shark
851 posts since Feb 2011
Reputation Points: 253
Solved Threads: 155
Skill Endorsements: 7

I'll test it out and see if I get the error again. It's strange because I explicitly followed the tutorial's instructions.

So if I understand this correctly... first it creates the superclass Pet and then creates the class Cat which automatically has the attributes defined in the superclass Pet?

If I want to add attributes to the subclass Cat do I simply re-define it using attr_accessor ?

Tupinambiss
Newbie Poster
2 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

The sub class inherits all attribute from the parent. For instance:

class Pet 
   attr_accessor :name
end

class Cat < Pet
end

c = Cat.new
c.name = "Freddy"    # <= Okay
c.weight = 32        # <= Error, no method defined
L7Sqr
Practically a Posting Shark
851 posts since Feb 2011
Reputation Points: 253
Solved Threads: 155
Skill Endorsements: 7

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.5240 seconds using 2.69MB