Skip to content

Instantly share code, notes, and snippets.

@junegunn
Last active October 14, 2017 07:04
Show Gist options
  • Save junegunn/8c3796a965f22e6a803fe53096ad7a75 to your computer and use it in GitHub Desktop.
Save junegunn/8c3796a965f22e6a803fe53096ad7a75 to your computer and use it in GitHub Desktop.
only-dir and with-dir
#!/usr/bin/env ruby
require 'set'
base = ARGV.first || '.'
dirs = Set.new
$stdin.each_line do |line|
dir = File.dirname(line)
anc = []
while dirs.add?(dir)
anc << dir
break if dir == base
dir = File.dirname(dir)
end
puts anc.reverse unless anc.empty?
end
#!/usr/bin/env ruby
require 'set'
base = ARGV.first || '.'
dirs = Set.new
$stdin.each_line do |line|
dir = File.dirname(line)
anc = []
while dir != base && dirs.add?(dir)
anc << dir
dir = File.dirname(dir)
end
puts anc.reverse unless anc.empty?
print line
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment