Reputation: 63
If I have an array of strings in ruby and I want to get the resulting array, what is the best way of doing it?
array = ["string_apple", "string_banana", "string_orange"]
desired_result = [["string", "apple"], ["string", "banana"], ["string", "orange"]]
My current attempt:
array.map(|r| r.split("_"))
Upvotes: 3
Views: 698
Reputation: 12377
You need map{...}
, not map(...)
for correct syntax in Ruby here:
array = ["string_apple", "string_banana", "string_orange"]
# Assign to a different array:
split_array = array.map{ |s| s.split(/_/) }
# Or modify the original array in place:
array.map!{ |s| s.split(/_/) }
# [["string", "apple"], ["string", "banana"], ["string", "orange"]]
Upvotes: 3