oo1xlw
Last Updated: August 14, 2018
·
57.53K
· stevennunez

Ruby: Convert a string to an array of characters

"This is the bee's knees".scan /\w/
#=> ["T", "h", "i", "s", "i", "s", "t", "h", "e", "b", "e", "e", "s", "k", "n", "e", "e", "s"]

String's #scan will look for anything matching your pattern and return it in an array.

10 Responses
Add your response

4102

You can also do "test".split("")

over 1 year ago ·
4296

That would include spaces, the scan only returns what matches the regex. Nice find!

over 1 year ago ·
15722

Thank You. I used .scan instead of .split to make a small improvement in this 'Rubymonk module' lesson (second code from the bottom of the page)

https://rubymonk.com/learning/books/1-ruby-primer/chapters/35-modules/lessons/80-modules-as-namespaces#solution4151

over 1 year ago ·
15758

"This is the bee's knees".chars will do the job

over 1 year ago ·
15759

"This is the bee's knees".chars also includes spaces. Using #scan does not include them.

over 1 year ago ·
16451

Thank you this was helpful - I actually needed the spaces so thanks @maycry for the .chars tip

over 1 year ago ·
21230
"This is the bee's knees".chars.to_a
over 1 year ago ·
29363

"This is the bee's knees".tr(" ' ", "").split.join('').split('') # All chars that may need to be removed from string may be placed in 1st parm of #String.tr method

11 months ago ·
30057

Washer dryer combinations that utilize warmth http://builder.ezywebs.com.au/TrendWashersandDryer/TrendWashersandDryer/ pump innovation to completely dry like your https://sites.google.com/site/bestwashersanddryers/ air conditioner cleaning efficiency is comparable https://bestwashersanddryersicons.tumblr.com/ to an equal stand-alone home appliance https://bestwashersanddryerstool.wordpress.com half the footprint of two separate devices. http://washersanddryerssuggestion.angelfire.com/ you could clean as well as dry your garments

3 months ago ·
30285

performance of specific versions http://www.topdishwasherbrand-radamora.sitew.in/#Choose_Dishwasher.B use of the machines long-lasting. https://bestdishwashersuggetion.hatenablog.com/ close together to handle the average plate. https://bestdishwashermodel.kinja.com/ A lot of" in almost every group in J.D. Power's 2017 study. http://dishwasheruse.jigsy.com/ As you advance through the lines, http://bestdishwasheruser.bravesites.com/ we think the 500 uses one of the.

about 2 months ago ·