# Included modules and String

**URL:** <https://www.ruby-forum.com/t/included-modules-and-string/54548>\
**Category:** Ruby\
**Created:** [December 28, 2005, 5:05pm UTC](https://www.ruby-forum.com/t/included-modules-and-string/54548 "2005-12-28T17:05:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/35a633/32.png) [@rich](https://www.ruby-forum.com/u/rich)\
**Post date:** [December 28, 2005, 5:05pm UTC](https://www.ruby-forum.com/t/included-modules-and-string/54548/1 "2005-12-28T17:05:12Z")

</div>

I looked at the RDoc for the String class and found that the class  
includes the Enumerable module. I’d have thought that meant that you  
could call methods like inject or collect on a string, the method  
would iterate over all the characters in the string.

However, I get the following instead:

> “12345”.collect { |x| x.to\_i}  
> ==\> [12345] # instead of the expected [1,2,3,4,5].

Am I misunderstanding how include works in this case? Thanks alot.

-Rich

---

<div class="post-metadata">

**Author:** ![rich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/35a633/32.png) [@rich](https://www.ruby-forum.com/u/rich)\
**Post date:** [December 28, 2005, 5:11pm UTC](https://www.ruby-forum.com/t/included-modules-and-string/54548/2 "2005-12-28T17:11:16Z")

</div>

On Dec 28, 2005, at 10:03 AM, Rich wrote:

> Am I misunderstanding how include works in this case? Thanks alot.  
> String iterates over lines of text by default, but that’s easily  
> changed:

> > require “enumerator”  
> > =\> true
> 
> > “12345”.enum\_for(:each\_byte).map { |byte| byte - ?0 }  
> > =\> [1, 2, 3, 4, 5]

Hope that helps.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![rich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/35a633/32.png) [@rich](https://www.ruby-forum.com/u/rich)\
**Post date:** [December 28, 2005, 5:32pm UTC](https://www.ruby-forum.com/t/included-modules-and-string/54548/3 "2005-12-28T17:32:36Z")

</div>

Awesome. Thanks alot.

---

<div class="post-metadata">

**Author:** ![rich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/35a633/32.png) [@rich](https://www.ruby-forum.com/u/rich)\
**Post date:** [December 28, 2005, 8:23pm UTC](https://www.ruby-forum.com/t/included-modules-and-string/54548/4 "2005-12-28T20:23:36Z")

</div>

Rich wrote:

> I looked at the RDoc for the String class and found that the class  
> includes the Enumerable module. I’d have thought that meant that you  
> could call methods like inject or collect on a string, the method  
> would iterate over all the characters in the string.
> 
> However, I get the following instead:
> 
> > “12345”.collect { |x| x.to\_i}  
> > ==\> [12345] # instead of the expected [1,2,3,4,5].
> 
> Am I misunderstanding how include works in this case? Thanks alot.

The reason for this is that Enumerable merely wraps the  
object’s #each method. In the case of String, #each by  
default splits the string at each line (as determined  
by the record separator constant $/).

Using Enumerator works around this nicely 🙂

> -Rich

E
