TypeError: superclass mismatch for class Word in 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.

Tags:

Ruby