类型错误:Ruby 中 Word 类的超类不匹配 [英] TypeError: superclass mismatch for class Word in Ruby

查看:39
本文介绍了类型错误:Ruby 中 Word 类的超类不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 Word 类,但出现错误:

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

TypeError: 类 Word 的超类不匹配

TypeError: superclass mismatch for class Word

这是irb代码:

irb(main):016:0> class Word
irb(main):017:1>  def palindrome?(string)
irb(main):018:2>   string == string.reverse
irb(main):019:2>  end
irb(main):020:1> end
=> nil
irb(main):021:0> w = Word.new
=> #<Word:0x4a8d970>
irb(main):022:0> w.palindrome?("foobar")
=> false
irb(main):023:0> w.palindrome?("level")
=> true
irb(main):024:0> class Word < String
irb(main):025:1>  def palindrome?
irb(main):026:2>    self == self.reverse
irb(main):027:2>  end
irb(main):028:1> end
TypeError: superclass mismatch for class Word
 from (irb):24
 from C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.1/lib/rails/commands/console.rb:47:in `start'
 from C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.1/lib/rails/commands/console.rb:8:in `start'
 from C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.1/lib/rails/commands.rb:41:in `<top (required)>'
 from script/rails:6:in `require'
 from script/rails:6:in `<main>'

推荐答案

irb 的拇指规则(irbrails console)

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

如果你用继承创建了同一个类(超类),退出irb实例并再次创建它.为什么这个?因为否则会发生类冲突.

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.

在您的情况下,您使用的是 Windows(从问题中找到),因此只需在 DOS 提示符下键入 exit 并再次键入 irbrails console 并创建您的 Word 类,它应该可以工作.如果它不适合您,请告诉我.

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.

这篇关于类型错误:Ruby 中 Word 类的超类不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆