There is an array of strings
paths = ['foo/bar_baz/_sunny', bar/foo_baz/_warm', 'foo/baz/_cold', etc etc]
I need to remove underscore in each last part of path (_sunny => sunny, _warm => warm, _cold => cold)
paths.each do |path|
path_parts = path.split('/')
path_parts.last.sub!(/^_/, '')
puts path_parts.join('/')
end
However that solution is a bit dirty. I feel it can be done without using path.split and path.join. Do you have any ideas?
Thanks in advance
I don’t know Ruby, but the pattern
could be replaced with
if
$xis used in Ruby to reference matching groups, andgis valid flag. It would need to be applied once to the string, with no splits or joins.