Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Rob Munoz
PLUS
Rob Munoz
Courses Plus Student 586 Points

Split One Argument Into Multiple?

Is there a way to take one argument (lets say an output from a .read) and split it into multiple arguments?

2 Answers

I've found that the ruby "split" function has been useful.

Below are some examples from irb. Split defaults to splitting on spaces, but it will also accept another character like ",". And it will also take a regular expressions (yes they implement the dark side of the force) The last example's regular expression /,\s|,|\s/ says to split on 1) comma followed by a space or 2) a comma or 3) a space.

irb(main):001:0> "abc 123 xy".split => ["abc", "123", "xy"]

irb(main):002:0> "abc,123,xyz".split(",") => ["abc", "123", "xyz"]

irb(main):008:0> "abc, 123, xyz".split(/,\s|,|\s/) => ["abc", "123", "xyz"]

Rob Munoz
PLUS
Rob Munoz
Courses Plus Student 586 Points

Awesome, thanks. I haven't done much with regular expressions but seems like a good time to learn! Thanks so much!