Reputation:
For the project I am doing, I need to make pairs out of the input, but I can't figure out how, I could use some help.
How it is:
2209222717080109
How I want it to become:
["22","09","22","27","17","08","01","09"]
Upvotes: 1
Views: 879
Reputation: 110755
"2209222717080109".scan /../
#=> ["22", "09", "22", "27", "17", "08", "01", "09"]
Upvotes: 4
Reputation: 48428
Convert it to a string, convert that into an array of characters, then take each consecutive slice of two characters, and join each of those slices together:
2209222717080109.to_s.chars.each_slice(2).map(&:join)
#=> ["22", "09", "22", "27", "17", "08", "01", "09"]
Upvotes: 2
Reputation: 174
input = "2209222717080109"
input.chars.each_slice(2).map(&:join)
["22", "09", "22", "27", "17", "08", "01", "09"]
Upvotes: 3