Ruby 中的 str.each 不起作用 [英] str.each in Ruby isn't working

查看:40
本文介绍了Ruby 中的 str.each 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Ruby.

I'm Learning Ruby.

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

I found the method String#each at http://ruby-doc.org/core/classes/String.html.

当我尝试使用它时...

When I try using it...

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

...但我明白了NoMethodError.

我使用的是 Ruby 1.9.1p253,所以我不认为我使用的是旧版本.怎么回事?

I'm using Ruby 1.9.1p253 so I don't think I'm using an old version. What's going on?

推荐答案

Ruby 1.9 的 String 类不再有 each.使用 each_chareach_line 取决于你想要做什么.Ruby 1.9 String 的文档是 http://ruby-doc.org/core-1.9.3/String.html.

Ruby 1.9 no longer has each on the String class. Use either each_char or each_line depending on what you want to do. The docs for Ruby 1.9 String are http://ruby-doc.org/core-1.9.3/String.html.

这篇关于Ruby 中的 str.each 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆