简单的Ruby代码是';t运行

简单的Ruby代码是';t运行,ruby,Ruby,有人能告诉我为什么这不管用吗?下面的代码是文件prog.rb class String def to_b return true if self == "true" false end end 以下是错误: path/prog.rb:1: syntax error, unexpected keyword_def, expecting <' or ';' or '\n' return true if self =..

有人能告诉我为什么这不管用吗?下面的代码是文件
prog.rb

class String
    def to_b
        return true if self == "true"
        false
    end
end
以下是错误:

path/prog.rb:1: syntax error, unexpected keyword_def, expecting
<' or ';' or '\n'
             return true if self =...
                ^
path/prog.rb:1:语法错误,意外关键字_def,应为

这可能是正确的做法:

class String
    def to_b
        return (self == "true")
    end
end

我猜你的编辑器是如何保存回车的。它的意思是它需要一个
顺便说一句,你可以把这个方法简单地写成
def to_b;self==“真”;结束
。工作正常;怀疑你的编码或古怪的字符。你用什么来编辑它?肯定有可能。它不工作,你在什么平台上,用什么编辑.rb文件?@bob我在Windows 7上,用记事本++。@DylanMarkow是的,我想我不小心把它设置为其中一个-CR或LF,而不是CR+LF。只需复制并粘贴到另一个文件即可修复:)