I am currently using the following code:
text = "a, 1; b, 2"
temp = []
text.gsub(/\s+/, "").split(";").each {|x| temp << x.split(",")}
temp # => [["a","1"],["b","2"]]
I’m wondering if there is slick way to get this done all in one line without the extra temp = [] line? How would one join the result of each block execution into an array?
Thanks!
The Enumerable module is a must-read for any Ruby novice. Use Enumerable#map instead:
It appears you’re not familiar with the functional programming principles, here it’s a link it may help you (specifically: see this). Every time you write a
eachthink if there is some alternative (it’s ok to do a side-effect like writing a file, but it’s usually a bad idea to update a variable).