Find Out What Dependencies A Ruby Gem Has, Gem Dependency

Earlier this afternoon I was working on a development server that has a web server, a PostgreSQL database, Ruby, and rubygems installed on it. I was curious to find out if a specific gem I was about to install had any dependencies or other gems that would also be installed as dependents of the gem I wanted to test. Below I describe how to find out the dependencies of a specific gem by querying the remote server but also how to download a specific gem version to see what gems are dependents.

Query Remote Ruby Gem Server To Find Out Gem Dependencies:

Locating the dependencies of a gem is fairly easy using the below syntax though you may run into issues on older gems so following the below syntax is a second example where the gem can be downloaded and then analyzed for dependencies.

bash

  1. [root@dev gems]# gem dependency ar_mailer --remote
  2. Gem ar_mailer-1.3.1
  3.   hoe (>= 1.2.2, runtime)
  4.  
  5. Gem ar_mailer-1.5.1
  6.   rubyforge (>= 2.0.3, development)
  7.   gemcutter (>= 0.2.1, development)
  8.   minitest (>= 1.5.0, development)
  9.   minitest (~> 1.3, development)
  10.   hoe (>= 2.5.0, development)
  11.  
  12. Gem ar_mailer_generator-1.0.0
  13.   ar_mailer (> 0.0.0, runtime)
  14.   hoe (>= 1.2.2, runtime)
  15.  
  16. Gem ar_mailer_rails3-2.1.10
  17.  
  18. Gem ar_mailer_service-0.1.1
  19.   win32-service (= 0.5.2, runtime)
  20.   ar_mailer (>= 1.3.1, runtime)
  21.   hoe (>= 1.4.0, runtime)
  22.  
  23. [root@dev gems]#

I used ar_mailer in the example above because it provides a real world view of what you might see when attempting to query a remove server to see what dependencies a specific gem has. We are most interested in the first two sections of data that are returned including the dependencies for ar_mailer 1.3.1 and the dependencies for ar_mailer 1.5.1. Notice how the older version of the ar_mailer gem only requires the hoe gem however the newer version of ar_mailer requires hoe, minitest, gemcutter, and rubyforge. You can also query a remote server about a the dependencies of a specific gem version as shown below.

Query Remote Gem Server About The Dependencies Of A Specific Gem Version:

bash

  1. [root@dev gems]# gem dependency ar_mailer --remote --version="1.3.1"
  2. Gem ar_mailer-1.3.1
  3.   hoe (>= 1.2.2, runtime)
  4.  
  5. [root@dev gems]# gem dependency ar_mailer --remote --version="1.5.1"
  6. Gem ar_mailer-1.5.1
  7.   rubyforge (>= 2.0.3, development)
  8.   gemcutter (>= 0.2.1, development)
  9.   minitest (>= 1.5.0, development)
  10.   minitest (~> 1.3, development)
  11.   hoe (>= 2.5.0, development)
  12.  
  13. [root@dev gems]#

As you can see above there are two examples to show that the remote server is returning the correct dependencies based on the gem version we are querying about. Now if the remote server doesn’t return any results or states that the gem is unknown as shown below then you can instead download the gem and query the gem file itself to obtain the list of dependencies.

Gem Server: ERROR Unknown gem

bash

  1. [root@dev gems]# gem specification treetop --version="1.2.4" --remote
  2. ERROR:  Unknown gem 'treetop'
  3. [root@dev gems]#

Download Gem Using Gem Fetch:

bash

  1. [root@dev gem-test]# gem fetch treetop
  2. Downloaded treetop-1.4.3
  3. [root@dev gem-test]# gem fetch treetop --version="1.2.4"
  4. Downloaded treetop-1.2.4

Again there are two examples above. The first shows that the gem server will always return the latest gem version if the version is not specified and the second example displayed above shows that you can specify a gem version using the –version switch. After issuing the “gem fetch” command a .gem file will have been downloaded. In the above example treetop-1.4.3.gem and treetop-1.2.4.gem were both downloaded and you can use “gem specification <filename>” to find out what the dependencies are as shown in the below example.

Query Gem File For List Of Gem Dependencies:

bash

  1. [root@dev gem-test]# gem specification treetop-1.2.4.gem
  2. --- !ruby/object:Gem::Specification
  3. name: treetop
  4. version: !ruby/object:Gem::Version
  5.   version: 1.2.4
  6. platform: ruby
  7. authors:
  8. - Nathan Sobo
  9. autorequire: treetop
  10. bindir: bin
  11. cert_chain: []
  12.  
  13. date: 2008-06-01 09:00:00 -05:00
  14. default_executable:
  15. dependencies:
  16. - !ruby/object:Gem::Dependency
  17.   name: polyglot
  18.   version_requirement:
  19.   version_requirements: !ruby/object:Gem::Requirement
  20.     requirements:
  21.     - - &quot;&gt;=&quot;
  22.       - !ruby/object:Gem::Version
  23.         version: &quot;0&quot;
  24.     version:
  25. description:
  26. email: nathansobo@gmail.com
  27. executables:
  28. - tt
  29. extensions: []
  30.  
  31. extra_rdoc_files: []
  32.  
  33. files:
  34. - README
  35. - Rakefile
  36. - lib/metagrammar.rb
  37. - lib/treetop
  38. - lib/treetop/bootstrap_gen_1_metagrammar.rb
  39. - lib/treetop/compiler
  40. - lib/treetop/compiler/grammar_compiler.rb
  41. - lib/treetop/compiler/lexical_address_space.rb
  42. - lib/treetop/compiler/metagrammar.rb
  43. - lib/treetop/compiler/metagrammar.treetop
  44. - lib/treetop/compiler/node_classes
  45. - lib/treetop/compiler/node_classes/anything_symbol.rb
  46. - lib/treetop/compiler/node_classes/atomic_expression.rb
  47. - lib/treetop/compiler/node_classes/character_class.rb
  48. - lib/treetop/compiler/node_classes/choice.rb
  49. - lib/treetop/compiler/node_classes/declaration_sequence.rb
  50. - lib/treetop/compiler/node_classes/grammar.rb
  51. - lib/treetop/compiler/node_classes/inline_module.rb
  52. - lib/treetop/compiler/node_classes/nonterminal.rb
  53. - lib/treetop/compiler/node_classes/optional.rb
  54. - lib/treetop/compiler/node_classes/parenthesized_expression.rb
  55. - lib/treetop/compiler/node_classes/parsing_expression.rb
  56. - lib/treetop/compiler/node_classes/parsing_rule.rb
  57. - lib/treetop/compiler/node_classes/predicate.rb
  58. - lib/treetop/compiler/node_classes/repetition.rb
  59. - lib/treetop/compiler/node_classes/sequence.rb
  60. - lib/treetop/compiler/node_classes/terminal.rb
  61. - lib/treetop/compiler/node_classes/transient_prefix.rb
  62. - lib/treetop/compiler/node_classes/treetop_file.rb
  63. - lib/treetop/compiler/node_classes.rb
  64. - lib/treetop/compiler/ruby_builder.rb
  65. - lib/treetop/compiler.rb
  66. - lib/treetop/ruby_extensions
  67. - lib/treetop/ruby_extensions/string.rb
  68. - lib/treetop/ruby_extensions.rb
  69. - lib/treetop/runtime
  70. - lib/treetop/runtime/compiled_parser.rb
  71. - lib/treetop/runtime/interval_skip_list
  72. - lib/treetop/runtime/interval_skip_list/head_node.rb
  73. - lib/treetop/runtime/interval_skip_list/interval_skip_list.rb
  74. - lib/treetop/runtime/interval_skip_list/node.rb
  75. - lib/treetop/runtime/interval_skip_list.rb
  76. - lib/treetop/runtime/syntax_node.rb
  77. - lib/treetop/runtime/terminal_parse_failure.rb
  78. - lib/treetop/runtime/terminal_syntax_node.rb
  79. - lib/treetop/runtime.rb
  80. - lib/treetop.rb
  81. - bin/tt
  82. - doc/contributing_and_planned_features.markdown
  83. - doc/grammar_composition.markdown
  84. - doc/index.markdown
  85. - doc/pitfalls_and_advanced_techniques.markdown
  86. - doc/semantic_interpretation.markdown
  87. - doc/site.rb
  88. - doc/sitegen.rb
  89. - doc/syntactic_recognition.markdown
  90. - doc/using_in_ruby.markdown
  91. - examples/lambda_calculus
  92. - examples/lambda_calculus/arithmetic.rb
  93. - examples/lambda_calculus/arithmetic.treetop
  94. - examples/lambda_calculus/arithmetic_node_classes.rb
  95. - examples/lambda_calculus/arithmetic_test.rb
  96. - examples/lambda_calculus/lambda_calculus
  97. - examples/lambda_calculus/lambda_calculus.rb
  98. - examples/lambda_calculus/lambda_calculus.treetop
  99. - examples/lambda_calculus/lambda_calculus_node_classes.rb
  100. - examples/lambda_calculus/lambda_calculus_test.rb
  101. - examples/lambda_calculus/test_helper.rb
  102. has_rdoc: false
  103. homepage: http://functionalform.blogspot.com
  104. licenses:
  105. post_install_message:
  106. rdoc_options: []
  107.  
  108. require_paths:
  109. - lib
  110. required_ruby_version: !ruby/object:Gem::Requirement
  111.   requirements:
  112.   - - &quot;&gt;=&quot;
  113.     - !ruby/object:Gem::Version
  114.       version: &quot;0&quot;
  115.   version:
  116. required_rubygems_version: !ruby/object:Gem::Requirement
  117.   requirements:
  118.   - - &quot;&gt;=&quot;
  119.     - !ruby/object:Gem::Version
  120.       version: &quot;0&quot;
  121.   version:
  122. requirements: []
  123.  
  124. rubyforge_project:
  125. rubygems_version: 1.3.5
  126. signing_key:
  127. specification_version: 2
  128. summary: A Ruby-based text parsing and interpretation DSL
  129. test_files: []
  130.  
  131. [root@dev gem-test]#

The above information should provide enough information to locate the gem dependencies of any gem regardless of how old the gem is.

Ruby Cookbook (Cookbooks (O’Reilly)) (Paperback)


List Price: $49.99
New From: $20.00 USD In Stock
Used from: $0.13 USD In Stock

Distributed Programming with Ruby (Paperback)


List Price: $44.99 USD
New From: $28.75 USD In Stock
Used from: $1.33 USD In Stock

Share