Forum: Ruby Re: Included modules and String

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
=?utf-8?B?UGXDsWEsIEJvdHA=?= (Guest)
on 2005-12-29 03:25
(Received via mailing list)
#On Behalf Of Eero S.

#>>"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 $/).

is it possible to pass a param for collect/map to signal the separator
(akin to split)? eg

>"12345".collect(//) { |x| x.to_i}
==> [1, 2, 3, 4, 5]

and noting that

> "12345".split(//).collect{|x| x.to_i}
=> [1, 2, 3, 4, 5]

kind regards -botp
This topic is locked and can not be replied to.