TypeError: superclass mismatch for class Word in Ruby

前端 未结 4 1337
生来不讨喜
生来不讨喜 2020-12-05 17:37

I am creating a Word class and I am getting an error:

TypeError: superclass mismatch for class Word

Here is the

4条回答
  •  情歌与酒
    2020-12-05 18:16

    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.

提交回复
热议问题