Alex Rivera | Logout

str.each in Ruby isn't working

Asked 2010-01-20T19:37:15.947
23

I'm Learning Ruby.

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

...but I get that NoMethodError.

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

Edit
Report

2 Answers

10

Use each_char instead:

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

as to why it's not working, it works in 1.8 but not 1.9.

answered 2010-01-20T19:39:56.547
2

Works in 1.8.7, doesn't show up in the 1.9 docs -- must have been deprecated.

Here we go: https://web.archive.org/web/20090423003136/http://eigenclass.org/hiki.rb?Changes+in+Ruby+1.9#l113

answered 2010-01-20T19:39:29.313

Your Answer