Skip to content

Instantly share code, notes, and snippets.

@agibralter
Created July 8, 2010 21:19
Show Gist options
  • Save agibralter/468653 to your computer and use it in GitHub Desktop.
Save agibralter/468653 to your computer and use it in GitHub Desktop.
Given an array of path strings, find the longest common prefix path.
# Given an array of path strings, find the longest common prefix path.
def find_base_path(paths)
return paths.first if paths.length <= 1
arr = paths.sort
f = arr.first.split('/')
l = arr.last.split('/')
i = 0
i += 1 while f[i] == l[i] && i <= f.length
f.slice(0, i).join('/')
end
@mamantoha
Copy link

Thank you! You've made my day.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment