Ruby中的str.each不起作用

我正在学习Ruby。

我在http://ruby-doc.org/core/classes/String.html找到了方法String#each

当我尝试使用它时……

 irb(main):001:0> "hello\nworld".each {|s| ps} NoMethodError: undefined method `each' for "hello\nworld":String 

…但我得到了NoMethodError

我正在使用Ruby 1.9.1p253所以我不认为我使用的是旧版本。 这是怎么回事?

Ruby 1.9上不再有each String类。 根据您的要求,使用each_chareach_char 。 Ruby 1.9 String的文档是http://ruby-doc.org/core-1.9.3/String.html 。

请改用each_char

 "hello\nworld".each_char {|s| ps} 

至于为什么它不起作用,它适用于1.8而不是1.9。

你正在看1.8的文档。 String#each已在1.9中删除。 请改用each_line。

在1.8.7中工作,未在1.9文档中显示 – 必须已弃用。

我们走了: https : //web.archive.org/web/20090423003136/http : //eigenclass.org/hiki.rb?Changes+in+Ruby+1.9#l113