gpt4 book ai didi

ruby - 为什么单词 'translate' 会干扰 irb?

转载 作者:数据小太阳 更新时间:2023-10-29 08:43:26 25 4
gpt4 key购买 nike

我不明白为什么我的方法 translate 取消定义 start_with? 方法并且在 irb 中弄乱了一些东西,所以我只能通过按 Ctrl< 退出 irb/kbd>+d,不是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 库使用,所以这是我唯一的怀疑,因此这个问题的标题。然而,作为初学者,我看不出有任何关系。

最佳答案

这是 YARV 中的错误 fixed在 YARV 2.4.0 中。

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

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

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

关于ruby - 为什么单词 'translate' 会干扰 irb?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41590976/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com