Skip to content

Instantly share code, notes, and snippets.

Last active October 5, 2020 15:21
Show Gist options
  • Save nixpulvis/5042764 to your computer and use it in GitHub Desktop.
Save nixpulvis/5042764 to your computer and use it in GitHub Desktop.
Remove all non default gems. For ruby 2.0.0
#!/usr/bin/env ruby
# Remove all gems EXCEPT defaults :)
`gem list -d`.split(/\n\n^(?=\w)/).each do |data|
match = data.match(/(?<name>([^\s]+)) \((?<versions>.*)\)/)
name = match[:name]
versions = match[:versions].split(', ')
if match = data.match(/^.*\(([\d\.]*),? ?default\): .*$/)
next if match[1].empty? # it's the only version if this match is empty
versions.delete(match[1] || versions[0])
versions.each { |v| system "gem uninstall -Ix #{name} -v #{v}" }
Copy link

pvdb commented Sep 13, 2013

👍 ... works straight out of the box; thanks for sharing!

Copy link

Peeja commented Jan 16, 2014

GitHub appears to have changed the way raw URLs work.

Updated: Run without saving by copying one of these into your shell.

CURL: curl -Ls | ruby
WGET: wget -qO- | ruby

Copy link

-:4:in split': invalid byte sequence in US-ASCII (ArgumentError) from -:4:in


Copy link

Works fine, nice job, thank you!

Copy link

ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.

Copy link

zchee commented Apr 19, 2015

I'm fork and fixed.
Exclude my favorite gems during gem uninstall.

If you have a gem that want to keep, it is preferable to exclude them by fork.

curl -Ls | ruby

Copy link

pvdb commented Jun 4, 2018

@nixpulvis - any ideas/suggestions for not only skipping the so-called default gems, but also the so-called bundled gems? see for their respective definitions.

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