Included modules and String

Discussion in 'Ruby' started by Rich, Dec 28, 2005.

  1. Rich

    Rich Guest

    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}

    =3D=3D> [12345] # instead of the expected [1,2,3,4,5].

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

    -Rich
    Rich, Dec 28, 2005
    #1
    1. Advertising

  2. On Dec 28, 2005, at 10:03 AM, 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.


    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 Gray II
    James Edward Gray II, Dec 28, 2005
    #2
    1. Advertising

  3. 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


    --
    Posted via http://www.ruby-forum.com/.
    Eero Saynatkari, Dec 28, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Justin Standard
    Replies:
    0
    Views:
    263
    Justin Standard
    Feb 7, 2005
  2. Martin DeMello
    Replies:
    3
    Views:
    117
    Joel VanderWerf
    Nov 9, 2006
  3. Replies:
    2
    Views:
    97
  4. Dale
    Replies:
    1
    Views:
    110
  5. Mark Wilden
    Replies:
    5
    Views:
    185
    Rick DeNatale
    May 15, 2008
Loading...

Share This Page