Is there a way to convert a String to an Array? I could only find the following - 'hello world'.scan(/./) Thanks.
on 2007-03-16 14:18
on 2007-03-16 14:25
On 16/03/07, RubyCrazy <firstname.lastname@example.org> wrote: > Is there a way to convert a String to an Array? I could only find the > following - > 'hello world'.scan(/./) > > Thanks. > irb(main):001:0> "hello world".split(//) => ["h", "e", "l", "l", "o", " ", "w", "o", "r", "l", "d"] I'm not sure if this is functionally the same or if there is some kind of performance boost using split instead of scan. Farrel
on 2007-03-16 14:31
RubyCrazy wrote: > Is there a way to convert a String to an Array? I could only find the > following - > 'hello world'.scan(/./) And what's the problem with that? Or do you want a different result? Alternatively you can do 'hello world'.split(' ') or 'hello world'.split(/\s+/) if you want to split on any number of whitespace chars. If you want to split just on single spaces, your original solution is faster. Cheers, Peter __ http://www.rubyrailways.com :: Ruby and Web2.0 blog http://scrubyt.org :: Ruby web scraping framework http://rubykitchensink.ca/ :: The indexed archive of all things Ruby