[Rubygems-developers] query_command.rb question and patch

Eric Hodel drbrain at segment7.net
Sat Apr 5 22:43:52 EDT 2008


On Apr 3, 2008, at 21:23 PM, Jos Backus wrote:
> Puppet (http://reductivelabs.com/trac/puppet) uses the `gem list'  
> command as
> part of its gem package provider. I'm thinking about fixing the  
> provider to
> use the RubyGems API instead of the command line tool as the command  
> line tool
> produces output that isn't strictly related to the local or remote  
> gem list,
> and incurs extra output parsing.

If you're looking for the list of locally installed gems as strings,  
this is the best way:

Gem.source_index.map { |name, gemspec| name }

Gem::SourceIndex is Enumerable, so you can easily filter results if  
you need based on the gemspec object.

> E.g for the local gem list case query_command.rb uses
> Gem.source_index.search(name) to get a list of local gems. When  
> calling it I
> still see output related to updating the gem index. I'd like to  
> avoid this
> extra output; is that possible?

You shouldn't see anything about updating the index for a `gem list`  
or `gem query`, as those don't attempt to update the index.  (Only if  
the -r flag is given.)

> Also: the attached patch against 1.1.0 refactors  
> output_query_results into two
> methods, one that produces the list of gems with their versions and  
> another
> that outputs this list in the same format output_query_results does  
> today.
> This makes it easier to programmatically access the list of local  
> and remote
> gems.


I'd much rather improve the programmer API over adding extra things to  
Gem::Command subclasses, as those classes are at least one level of  
indirection away from whatever you probably want to do.

I can guide you towards the proper API calls to make if you have a  
specific goal, but I strongly recommend avoiding directly calling  
methods on Gem::Command subclasses.


More information about the Rubygems-developers mailing list