Skip to content

Instantly share code, notes, and snippets.

@ahoward
Created February 13, 2012 18:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahoward/1819027 to your computer and use it in GitHub Desktop.
Save ahoward/1819027 to your computer and use it in GitHub Desktop.
~/bin/gemdir
#! /usr/bin/env ruby
# usage: gemdir gemname
#
# eg: cd `gemdir yajl`
#
require 'rubygems'
arg = ARGV.first
unless arg
puts File.join(Gem.dir, "gems")
exit
end
which = arg.dup
which << '*' if which
entries =
Dir[ File.join(Gem.dir, "gems", "#{ which }") ].select{|entry| test ?d, entry}.sort
case entries.size
when 0
abort "no gem #{ which }"
when 1
puts entries.first
else
all_same_name = entries.map{|e| File.basename(e).split('-').first}.uniq.size == 1
if all_same_name
puts entries.last
else
unless STDIN.tty?
warn "ambiguous gemname #{ arg.inspect }"
end
abort(entries.join("\n"))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment