1

我正在做 Mike Hartl 的 Rails 教程和第 4.4.2 节“类继承”。具体来说,当尝试在 Rails 控制台中创建一个根据“清单 4.8”从 String 类继承的类时,我在执行它时收到以下错误。

irb(main):001:0> class Word < String
#<Class:0x56fc868>: undefined local variable or method `ti' for main:Object
        from (irb):25
        from d:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
        from d:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
        from d:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'
irb(main):026:0> class Word < String
#<Class:0x54a1178>: undefined local variable or method `asodSrlsW' for main:Object
        from (irb):1
        from d:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
        from d:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
        from d:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'

更奇怪的是......当我尝试再次执行同一行代码来创建类时,我想也许我只是有一个错字,我得到一个稍微不同的错误,这没有任何意义。由于我感到困惑,因此非常感谢对此的任何指导。

4

1 回答 1

0

我唯一一次看到这种行为是当我在 Eclipse 终端中使用控制台并使用 ctrl+c 关闭它时。在同一个终端中再次运行控制台会产生各种疯狂。

$ rails c
Loading development environment (Rails 3.0.4)
irb(main):001:0> class Word < String
irb(main):002:1> puts 'word test'
irb(main):003:1> end
word test
=> nil
irb(main):004:0> ^C
irb(main):004:0>

$ rails c
Loading development environment (Rails 3.0.4)
irb(main):001:0> class Word < String
NameError: uninitialized constant Stn
        from (irb):5
irb(main):006:0>

# At this point pressing cursor up to repeat last line gives garbled nonsense:
irb(main):006:0> la Wr Stn

唯一的解决方案是杀死该终端并重新开始。

于 2013-05-30T09:49:27.703 回答