RosettaCodeData/Task/Find-common-directory-path/Ruby/find-common-directory-path-1.rb

7 lines
305 B
Ruby
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
require 'abbrev'
dirs = %w( /home/user1/tmp/coverage/test /home/user1/tmp/covert/operator /home/user1/tmp/coven/members )
common_prefix = dirs.abbrev.keys.min_by {|key| key.length}.chop # => "/home/user1/tmp/cove"
common_directory = common_prefix.sub(%r{/[^/]*$}, '') # => "/home/user1/tmp"