3

When I wrote the following code, it runs normally:

class Application def initialize(name) @name = name end end class Email2 < Application end 

But when I changed Email2 to Email like this:

class Application def initialize(name) @name = name end end class Email < Application end 

I got the error message: superclass mismatch for class Email. Please help me.

3
  • where are you coding..? I mean in the console? Commented Dec 3, 2015 at 11:20
  • Possible duplicate of Ruby on Rails 3 : "superclass mismatch for class ..." Commented Dec 3, 2015 at 11:21
  • i code on firefox browser? I code online on web Commented Dec 3, 2015 at 11:21

2 Answers 2

8

The Email class must already be defined somewhere else.

You can test that by using the defined? method like this:

defined?(Email) 

Think about namespacing your code by using a module:

module MyNameSpace class MyClass end end 

Looks like you need to remove the definition from the CodeAcademy Context. Try deleting your browser cookies and refreshing the page.

Sign up to request clarification or add additional context in comments.

10 Comments

When i add "defined?(Email)" at the end of my code, it print out ""constant" But that is studying website, and that is a problem which i must resolve. That is my mission: "Create a second class, Email, that inherits from Message. Give it its own initialize method that takes just one parameter, subject, and has one instance variable, @subject, set equal to subject." How to resolve it? I want to jump to next lesion ?
Can you send me the link to the lesson?
codecademy.com/courses/ruby-beginner-en-MFiQ6/3/… But i think you must start from the beginning. Thanks u
@Viet note that your Email class does not inherit from Message
@Viet, I think you can just refresh the page to clear the Email definition from the context.
|
4

The error occurs because there's already a class Email defined somewhere else, that inherits from something else than Application.

When using the class keyword, if the class already exists ruby will try to reopen the class, allowing you to add things to the existing class definition.

If you write class Email < Application, ruby will try to make Email inherit from Application. Ruby classes can't have more than one parent class, so if the existing Email class already inherits from something else, you will get this error.

To inherit from Message, you write this: class Email < Message

2 Comments

Yes, i have changed it from "Application" to "Message" And i understand why that bug is occurred. But i was studying on codecademy.com website. I don't know how must i do to pass this lesion: "Create a second class, Email, that inherits from Message. Give it its own initialize method that takes just one parameter, subject, and has one instance variable, @subject, set equal to subject.""
@Viet Perhaps this is not the correct forum to ask for solutions to study material. Maybe you should read up on Ruby basics a bit.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.