Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: superclass mismatch for class Word in Ruby

Tags:

ruby

A thumb rule for irb (either way irb or rails console)

If you are creating the same class twice with inheritance (superclass), exit the irb instance and create it again. Why this? Because otherwise class conflicts will happen.

In your case, you are using Windows (found from the question), so just type exit on DOS prompt and again type irb or rails console and create your Word class and it should work. Please let me know if it doesn't work for you.


The reason it gives you a superclass mismatch error is because you have already defined the Word class as inheriting from Object

class Word
...
end

In Ruby (like in most dynamic languages) you can monkey-patch classes by reopening the definition and modifying the class. However, in your instance, when you are reopening the class you are also attempting to redefine the class as inheriting from the super class String.

class Word < String
...
end

Once a class and it's inheritance structure have been defined, you cannot define it again.

As a few people have said, exiting and restarting irb will allow you to start from scratch in defining the Word class.


link664 has clearly explained the problem.

However, there's an easier fix without quitting irb (and losing all your other work). You can delete an existing class definition this way.

irb(main):051:0> Object.send(:remove_const, :Word)

and you can verify with:

irb(main):052:0> Word.public_instance_methods

which should return:

NameError: uninitialized constant Word
from (irb):52