I'm trying to modify an array of hashes using collect!. For each hash I want to add 1 new key/value and modify a different key/value. However, I'm having problems modifying the existing hash value using sub!. It seems to completely replace the hash with a single array entry equaling the result of the sub! command
paths = [{:path=>"bin/ruby/file1", :tag=>"v_10"}, {:path=>"usr/name/subdir/file2", :tag=>"v_12"}]
paths.collect! do |x|
x.merge(Hash[:file => x[:path].sub(/.*\//,"")]) # Grab file name
x[:path].sub!(/\/\w+$/,"") # remove file name from path
end
RESULT:=> ["bin/ruby", "usr/name/subdir"]
DESIRED RESULT:=> [{:path=>"bin/ruby", :tag=>"v_10", :file=>"file1"}, {:path=>"usr/name/subdir", :tag=>"v_12", :file=>"file2"}]