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

Regex Array Split with Ruby?

Is there an existing method to split array (not just a string) to multiple components by delimiters in Ruby?

I have struggled over the past few days to find a way around the fact that split converts a string to an array and thus cannot be split again after the first "layer" of parsing.

Thanks!

Can you give a concrete example of what you are trying to do?

Rob Munoz
Rob Munoz
Courses Plus Student 586 Points

Hi John, yeah I'll try!

Here: L0 = "0T00N00N00B00K00E00K00D"

L1 = L0.split("T")

L2 = L1.split("B")

L3 = L2.split("N")

Etc...

William Li
William Li
Courses Plus Student 26,868 Points

Hi, Rob, even with the example you provided (String split), I still don't quite get what you mean by Array split using Regex? Array is consisted of individual items, you can slice the Array, but that's pretty much it. It doesn't make sense to split array using Regex.

Rob Munoz
Rob Munoz
Courses Plus Student 586 Points

Hi William, Sorry about that. I guess what I'm looking to do is split multiple times. But I have been unable to do so because split converts datatypes (str->array) from L0 and L1. While I would like to continue splitting an array, I am unsure of how to do so.

WEll not all at once but if you really want to do all that splitting progressive Loop through the first array entries - split them again (come up with a nomenclature) then loop again - and again you could do it recursively via a function is you wanted to

still don't see the why of it though

WEll not all at once but if you really want to do all that splitting progressive Loop through the first array entries - split them again (come up with a nomenclature) then loop again - and again you could do it recursively via a function is you wanted to

still don't see the why of it though

Rob Munoz
Rob Munoz
Courses Plus Student 586 Points

Hey John, I think you're making a really important point. While storing each level as it's own array might be useful in some cases, it is not necessary to complete the given task. I'll show an example in a minute but thanks so much for the input!

2 Answers

I don't understand why you'd want to do this...you could loop through the resulting arrays and join them and then split them on the new split...you can daisy chain them with to_s in between to convert the array to the string but that outcome is weird looking.

Rob Munoz
Rob Munoz
Courses Plus Student 586 Points

Hi John! I want to store each result as it's own array so that I can have access to it later on. I also might end up performing operations like splitting the zeros between T and N. Sorry about how vague my post is. I'm new to ruby and programming in general.

Rob Munoz
PLUS
Rob Munoz
Courses Plus Student 586 Points

This is what I was trying to do

A = "0T00N00N00B00K00E00K00D"

B = A.split(/["T","N","B"]/)

C = B.join("")

puts C.class

D = C.split(/["B","E","K"]/)

puts D

Worked out perfectly.