Technology Entrepreneur, Innovator, Author and Thought Leader

Shashank Tiwari

Subscribe to Shashank Tiwari: eMailAlertsEmail Alerts
Get Shashank Tiwari via: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn

Blog Feed Post

Painlessly Remove All Ruby Gems on Windows

If you want to uninstall all gems from your local Ruby installation on a Linux, Unix or MacOSX box then you can rely on the standard shell commands like “cut” and “xargs”, to make the process easy and effortless. The command itself is a one liner as follows:

gem list | cut -d" " -f1 | xargs gem uninstall -aIx

Read this post titled — Painlessly Remove All Ruby Gems — to learn the details. If you are on Windows though, the usual “cut” and “xargs” are not available. What are the alternatives then? One old school method may be to write a batch file or a script to do the job. However, that method is both a bit clumsy and verbose for a task that could be achieved through a one liner elsewhere. A smarter option then is to use the Windows PowerShell. Lets see how.

First start a PowerShell instance. If you are on Windows 7, it would simply mean typing “PowerShell” (or even “powershell”) on your program search box and then selecting the “Windows PowerShell” program. Once a session is instantiated, type the usual

gem list

command to list all the installed gems. On my machine the output looks like so:

*** LOCAL GEMS ***
abstract (1.0.0)
actionmailer (3.0.0, 3.0.0.beta3, 2.3.5)
actionpack (3.0.0, 3.0.0.beta3, 2.3.5)
activemerchant (1.4.1)
activemodel (3.0.0, 3.0.0.beta3)
activerecord (3.0.0, 3.0.0.beta3, 2.3.5)
activerecord-tableless (0.1.0)
activeresource (3.0.0, 3.0.0.beta3, 2.3.5)
activesupport (3.0.0, 3.0.0.beta3, 2.3.5)
addressable (2.1.1)
arel (1.0.1, 0.3.3)
authlogic (2.1.3)
builder (2.1.2)
bundler (1.0.0, 0.9.25)
buzzr (0.2)
calendar_date_select (1.15)
cgi_multipart_eof_fix (2.5.0)
chronic (0.2.3)
compass (0.8.17)
couchrest (0.37)
crack (0.1.7)
data_objects (0.10.1)
dbd-mysql (0.4.3)
dbi (0.4.3)
deprecated (2.0.1)
dm-core (0.10.2)
do_mysql (0.10.1 x86-mswin32-60)

To get a list of all installed gems, one unique entry per line, and containing nothing other than the names one can use the “cut” command on a Unix/Linux/MacOSX machine. With PowerShell though

gem list | cut -d" " -f1

doesn’t work but

gem list | %{$_.split(' ')[0]}

does. The $_ passes the current variable value to the “split” command, which uses a delimiter to split a string. In the example above a space is the delimiter. The parts generated out of the splitting are available as members of an array. Accessing the element at the 0th index of this array returns the first element.

Now that we have the names of all the installed gems, we need to iterate over this list and invoke gem uninstall with flags Iax for each of these. The I is for ignore dependency, a is for all matching gems and x is for no confirmation required. In other words running

gem uninstall -Iax activerecord

should uninstall all gems matching the name — “activerecord” — without any required confirmation.  Using xargs it is easy to pass the current value to a command as one iterates over a list. However, running

gem list | cut -d" " -f1 | xargs gem uninstall -aIx

doesn’t get the job done as xargs is unknown to PowerShell. Don’t be disappointed though for PowerShell has a replacement for xargs and its an elegant one. The position of the $_ makes all the difference. So the one liner that removes all ruby gems is:

gem list | %{$_.split(' ')[0]} | %{gem uninstall -Iax $_ }

Isn’t that nice, simple and just a one liner!

Read the original blog entry...

More Stories By Shashank Tiwari

I am a technology entrepreneur, innovator, author and, as some say, a “thought leader”. I like to solve challenging computing problems, especially those that drive innovation. Being a polyglot programmer, I can program fluently in many languages, including Java, Python, C++, C, Ruby, ActionScript, JavaScript, Objective-C, Haskell, Scala, Clojure, PHP, Groovy, Lisp and Perl. I must admit that I like to learn programming languages and if there is a new interesting one coming, I wouldn’t be far behind getting to grips with it. Over the last many years I have built some cutting edge enterprise and consumer software applications, many of which have leveraged large data sets and the web based programming paradigms. This means I also know a lot about data bases and persistence. I am very conversant with relational databases, embedded databases, object databases, text based data and XML. Having leveraged web based programming paradigms, I have first hand experience with a lot of web development frameworks, including but not limited to Adobe Flex, Spring MVC, Rails, Grails and Django. Not to forget, I obviously have worked a lot with HTML, JS and CSS. My experience and interest are varied and diverse and range a wide spectrum of application development realms that include the server, the client and the middleware. Besides, programming, I am also deeply interested in mathematics and theoretical computer science. This motivates me to bring my knowledge of applied mathematics, statistical modeling, artificial intelligence and sometimes simply data structures, to good use, when I build applications. A couple of domains like financial mathematics and scientific computing seem to have been good fit for such expertise. I am an ardent supporter of open source software and try and contribute to open source code bases and causes. I like the plurality and variety that software development offers; the choice of programming languages, the abundant availability of tools and libraries, the existence of multiple operating systems and the possibility of varied software development methodologies. As a member of the technology community, I am an active contributor to the ever evolving software development languages, methodologies and standards. I am an expert group member on a number of JCP (Java Community Process) specifications, for example JSRs 274, 283, 299, 301 & 312, and have been recognized as an Adobe Flex Champion.I run and organize a few community events like Flex Camp Wall Street, Show Ramp and Polyglot World. I bring together all my expertise in terms of services and products via my primary venture, Treasury of Ideas LLC, in which I play the role of a Managing Partner. Treasury of Ideas LLC, through its focus on innovation and value optimization, offers many best of the breed services and products and has incubated many ideas to help translate them to reality. Our clients range from large enterprises, government agencies, not-for-profit organizations to promising new startups. I write regularly in many technical journals and magazines, present in seminars and mentor developers and architects. I have authored a few books, including Advanced Flex 3 (friends of Ed/APress, 2008) and Professional BlazeDS (Wrox/Wiley, 2009) , and am in the process of authoring a few more. You can learn all about my books and public talks by browsing through the Publish & Present page at www.shanky.org.