RosettaCodeData/Task/Find-common-directory-path/Ruby/find-common-directory-path-1.rb
2023-07-01 13:44:08 -04:00

6 lines
305 B
Ruby

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"