【问题标题】:Why word 'translate' is messing irb?为什么“翻译”这个词弄乱了irb?
【发布时间】:2017-05-26 05:59:45
【问题描述】:

我不明白为什么我的方法 translate 取消定义 start_with? 方法并且在 irb 中弄乱了一些东西,所以我只能通过按 Ctrl+d退出 irb >,不是exitquit

>> "hello".respond_to?(:start_with?)
=> true
>> def translate(string)                                                                                                                   
>>   if string.start_with?("a", "e", "i", "o", "u")
>>     string += "ay"                                                                                                                      
>> end
>> end
NoMethodError: undefined method `start_with?' for #<RubyVM::InstructionSequence:0x00000001d4c960>
        from (irb):3:in `translate'
        from /usr/local/rvm/rubies/ruby-2.3.0/bin/irb:11:in `<main>'
>> "hello".respond_to?(:start_with?)                                                                                                       
NoMethodError: undefined method `start_with?' for <RubyVM::InstructionSequence:irb_binding@(irb)>:RubyVM::InstructionSequence
        from (irb):3:in `translate'
        from /usr/local/rvm/rubies/ruby-2.3.0/bin/irb:11:in `<main>'
>> exit
NoMethodError: undefined method `start_with?' for <RubyVM::InstructionSequence:irb_binding@(irb)>:RubyVM::InstructionSequence
        from (irb):3:in `translate'
        from /usr/local/rvm/rubies/ruby-2.3.0/bin/irb:11:in `<main>'
>> quit
NoMethodError: undefined method `start_with?' for <RubyVM::InstructionSequence:irb_binding@(irb)>:RubyVM::InstructionSequence
        from (irb):3:in `translate'
        from /usr/local/rvm/rubies/ruby-2.3.0/bin/irb:11:in `<main>'
>>  

我尝试了两个不同的工作区,效果是一样的。
我的 Ruby 和 Rails 版本是:

~/workspace $ ruby -v
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]
~/workspace $ rails -v
Rails 4.2.2

来自my other question 我知道translate 这个词被许多I18N 库使用,所以这是我唯一的嫌疑人,因此是这个问题的标题。但是作为初学者,我看不到任何关系。

【问题讨论】:

  • 你的方法在 irb 中对我很有效。查看您的任何代码/库是否对 String 进行了猴子修补/改进。
  • 您说您的translate 方法未定义String#start_with?,但在您显示的成绩单中没​​有表明这一点。您如何确定这确实是正在发生的事情?
  • @w0lf 我读到了这个猴子补丁。老实说,我不知道如何检查这一点,但如果是因为某些 gem,我只在空白的 cloud9 工作区上安装了 rubyrailsrspecpry。 @JörgWMittag undefined method start_with?'`这不是你的意思吗?我试过puts String.methods,但无论我输入什么,输出总是一样的。我只能 ctrl+d 并重新启动 irb。我还在新鲜的 irb 上注意到了这一点:> def translate(s) > puts s > end #<:instructionsequence:0x00000000a9a2e0> => :translate 我在 Ruby-doc 中检查了 RubyVM,但现在这是一些神秘的魔法

标签: ruby translate irb rails-i18n


【解决方案1】:

这是一个理论

  • 您的设置中可能有一个全局函数 translate
  • irb 打印输出时,该函数以指令序列作为参数调用
  • 因此,当您重新定义 translate 时打印输出中断

NoMethodError: undefined method 并不意味着该方法已在全局范围内未定义,而是它被发送到一个不理解它的对象

你可以通过执行来检验我的理论

method(:translate)

如果你得到一个结果,那么 translate 已经定义了,你不能重新定义它!

现在如果你想知道是哪个gem定义了这个函数,安装pry gem(这是一个更好的irb)并使用$命令查看该方法的文件和源代码

# Use this command in pry to see location and source code
$ translate

【讨论】:

    【解决方案2】:

    这是 YARV 中的一个错误,在 YARV 2.4.0 中是 fixed

    如果您没有 YARV 2.4.0,提交消息会提到以下解决方法:

    class << RubyVM::InstructionSequence
      def translate; end
      undef translate
    end
    

    请注意,其他实现不受影响,只有 YARV。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-30
      • 2014-02-18
      • 2013-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多