---
title: "String#each missing in Ruby 1.9"
description: "One might have noticed that String#each is no more present in Ruby 1.9 and how to fix it. Practical notes and code examples."
url: "https://h3manth.com/scribe/stringeach-missing-in-ruby19/"
canonical_url: "https://h3manth.com/scribe/stringeach-missing-in-ruby19/"
markdown_url: "https://h3manth.com/scribe/blog/stringeach-missing-in-ruby19.md"
publishDate: "2012-07-25T00:00:00.000Z"
author: "Hemanth HM"
category: "ruby"
tags: ["ruby"]
word_count: 184
reading_time_minutes: 1
x_markdown_tokens: 379
content_signal: "ai-train=yes, search=yes, ai-input=yes"
---

# String#each missing in Ruby 1.9

> One might have noticed that String#each is no more present in Ruby 1.9 and how to fix it. Practical notes and code examples.

One might have noticed that `String#each` is no more present in Ruby 1.9:

```ruby
1.9.1 :002 > "hemanth".each
NoMethodError: undefined method `each' for "hemanth":String
```

Lately, was working on a huge code base where code had `String#each` assuming it will always get an array, as Ruby 1.8 has `String#each` and also `Array#each`, but it does not get Strings at times so had to fix them.

**Methods to fix:**

**1. Check and cast:**

```ruby
if !obj.kind_of?(Array)
  acl_descs = ["#{obj}"]
end
```

This was a bit tedious, some 'sed' could have helped, but stopped it there!

**2. Check and Create:**

```ruby
unless String.respond_to?(:each)
  class String
    def each
      self.split.each { |line| yield line }
    end
  end
end
```

This was worth doing, but was in search of an easier way!

**3. Aliasing for the win!**

`String#each_line` is the method that helped a lot, as it was present in both 1.8 and 1.9.

```ruby
class String 
  alias :each :each_line
end
```

Finally, removal of `String#each` from 1.9 is really meaningful, as it was ugly: `string.each { |each_what???| }`

So, Ruby 1.9 along with `each_line` has `each_word` and `each_char`.
