gem の概要をコマンドラインから調べる方法

この gem は何だろう?と思うことがしばしば。

今まで Alfred を使って The Ruby Toolbox や RubyGems を検索 していたけど、知りたい gem が何個もあると面倒。

コマンドラインからできないか調べてみた。

gem search --details --local ( gem search -dl )

gem search に --details オプションを付ければ概要を教えてくれます。

それと --local を付けるとローカルを検索するので速いです。

ちなみに --deatails と --local は -dl と省略できます。

あと gem search は部分マッチなので大量マッチすることがあります。gem search -d ^spork$ とすることもできます。 ^ や $ を付けないほうが新たな発見もあって楽しいけどね。

$ gem search -dl spork

*** LOCAL GEMS ***

spork (1.0.0rc3)
    Authors: Tim Harper, Donald Parish
    Homepage: http://github.com/sporkrb/spork
    Installed at: /home/oooooooo/.rbenv/versions/1.9.3-p429/lib/ruby/gems/1.9.1

    spork

gem specification spork description ( gem sp spork description )

で、spork は --details を spork としか教えてくれません。ひどい。 ( six も six とかしか教えてくれません )

http://rubygems.org/gems/spork では

A forking Drb spec server

とちゃんと出ているのにね。

ということで gem search --details ではなく gem specification gem description なら RubyGems と同じ情報を教えてくれます。

specification は sp と略せるけど、description は一字たりとも略せません。

$ gem sp spork description
--- A forking Drb spec server
...

余分な「---」や「...」は gem の仕様です。

summary と description

gem には specification という情報ファイルがあり、そこには summary と description があります。

gem sp spork すると spork の specification をすべて見ることができます。

$ gem sp spork
--- !ruby/object:Gem::Specification
name: spork
version: !ruby/object:Gem::Version
  version: 0.9.2
platform: ruby
authors:
- Tim Harper
- Donald Parish
autorequire:
bindir: bin
cert_chain: 
date: 2012-05-18 00:00:00.000000000 Z
dependencies: 
description: A forking Drb spec server
email:
- timcharper+spork@gmail.com
executables:
- spork
extensions: 
extra_rdoc_files:
- MIT-LICENSE
- README.rdoc
files:
- bin/spork
- MIT-LICENSE
- README.rdoc
homepage: http://github.com/timcharper/spork
licenses: 
metadata: {}
post_install_message:
rdoc_options:
- --main
- README.rdoc
require_paths:
- lib
required_ruby_version: !ruby/object:Gem::Requirement
  requirements:
  - - ! '>='
    - !ruby/object:Gem::Version
      version: '0'
required_rubygems_version: !ruby/object:Gem::Requirement
  requirements:
  - - ! '>='
    - !ruby/object:Gem::Version
      version: '0'
requirements: 
rubyforge_project: spork
rubygems_version: 2.0.3
signing_key:
specification_version: 4
summary: spork
test_files: 

gem sp spork の後ろに summary や description を指定すると、その部分だけ表示することができます。

まとめるとこんな感じ。

  • gem specification gem summary と gem search --details は summary を表示
  • gem specification gem description は description を表示

description の方が詳しそうなので gem specification gem description を alias でもして使えばよさそうですが、description を持たない gem があります。 例えば charlock_holmes とか sanitize とか treetop とか warden とかは summary だけで description を持ちません。

summary を持たない gem はないけど、spork や six のように説明になっていない summary が散見します。

geminfo

なので summary と description を同時に表示するやっつけ仕事なコマンドを .zshrc に登録しました。

function geminfo() {
  if test $# -eq 1; then
    gem spec $1 summary     | ruby -pe 'gsub(/(\-\-\-|\.\.\.\n)/, "")'
    gem spec $1 description | ruby -pe 'gsub(/(\-\-\-|\.\.\.\n)/, "")'
  else
    bundle list | awk '/\* .+ / { print $2; system("gem spec "$2" summary;gem spec "$2" description") }' | ruby -pe 'gsub(/(\-\-\-|\.\.\.\n)/, "")'
  fi
}

使い方はこんな感じ。

$ geminfo spork
 spork

 A forking Drb spec server

それと、bundle install 後のフォルダで geminfo とすると、bundle show 対象の gem のすべての説明が出ます。

GitLab で利用されている全ての gem の概要を知りたいならこうします。

$ git clone git://github.com/gitlabhq/gitlabhq.git
$ cd gitlabhq
$ bundle install
$ bundle show
$ geminfo
actionmailer
 Email composition, delivery, and receiving framework (part of Rails).

 Email on Rails. Compose, deliver, receive, and test emails using the familiar
  controller/view pattern. First-class support for multipart email and attachments.

actionpack
 Web-flow and rendering framework putting the VC in MVC (part of Rails).

 Web apps on Rails. Simple, battle-tested conventions for building and testing
  MVC web applications. Works with any Rack-compatible server.

activemodel
 A toolkit for building modeling frameworks (part of Rails).

 A toolkit for building modeling frameworks like Active Record and Active Resource.
  Rich support for attributes, callbacks, validations, observers, serialization, internationalization,
  and testing.
( 以下省略 )

余談

gem コマンドを gem help spe して調べている時に typo を発見しました。

> gem help sp
Usage: gem specification [GEMFILE] [FIELD] [options]

  Options:
    -v, --version VERSION            Specify version of gem to examine
        --platform PLATFORM          Specify the platform of gem to specification
        --[no-]prerelease            Allow prerelease versions of a gem
        --all                        Output specifications for all versions of
                                     the gem
        --ruby                       Output ruby format
        --yaml                       Output RUBY format
( 以下省略 )

「--yaml」の説明が Output YAML format ではなく Output RUBY format になっています。

typo 修正を Pull Request したところ、先日、取り込まれました。

https://github.com/rubygems/rubygems/commit/e158c1b4f2939b122d0493fef0d12c7293cd9e91

単なる typo 修正なんだけど、rubygems に commit できて嬉しいw