I have a string of integers that can be separated by a comma, hyphen or both. e.g.
str = '3,7-17,21'
I need to take those numbers and reduce them by 1 without losing their position in that string.
When I split, I lose commas and hyphens so their positions change.
Is there a nice way to turn my string into 2,6-16,20?
I started on the following but just ended up in a rabbit hole:
def reduce_hours(hours)
hours
.enum_for(:scan, /\d+/)
.map { Regexp.last_match(0) }
.map(&:to_i)
.map { |hour| hour - 1 }
.then { hours }
end
hours = '3,7-17,21'
It just returns the original string. When I debug, it does match the numbers individually and reduce them by 1. It just doesn't return those changes to the original string. Am I close with that snippet?