Reputation: 583
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.
Upvotes: 3
Views: 10390
Reputation:
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.
Upvotes: 8
Reputation: 4555
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
Upvotes: 4