{"id":969,"date":"2008-08-05T20:47:00","date_gmt":"2008-08-05T20:47:00","guid":{"rendered":"https:\/\/www.keenertech.com\/?p=969"},"modified":"2008-08-05T20:47:00","modified_gmt":"2008-08-05T20:47:00","slug":"introduction-to-rubygems","status":"publish","type":"post","link":"https:\/\/staging.keenertech.com\/?p=969","title":{"rendered":"Introduction to RubyGems"},"content":{"rendered":"\n<p>RubyGems is a package manager for Ruby code. It&#8217;s used to organize reusable code into a deliverable known as a &#8220;gem.&#8221; Everyone who has programmed in Ruby or used the Rails framework has probably installed a gem at one time or another. Even the Rails framework is packaged as a set of gems.<\/p>\n\n\n\n<p>The troubling thing is that many Ruby developers, and&nbsp;<em>most<\/em>&nbsp;Rails developers, are not taking full advantage of the power of gems. This article will explore some of the capabilities of RubyGems and illustrate how those capabilities can be used to support real-world Rails development efforts.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The Basics<\/h3>\n\n\n\n<p>RubyGems is the project which produced the well-known&nbsp;<em>gem<\/em>&nbsp;utility program. It shouldn&#8217;t surprise anyone that packages created by the utility are known as gems.<\/p>\n\n\n\n<p>A gem has some key characteristics:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It contains packaged code.<\/li>\n\n\n\n<li>It has a version associated with it.<\/li>\n\n\n\n<li>It may have dependencies on other gems.<\/li>\n<\/ul>\n\n\n\n<p>The&nbsp;<em>gem<\/em>&nbsp;command can be used to install a gem:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ gem install will_paginate<\/code><\/pre>\n\n\n\n<p>The example above installs the most recent version of the &#8220;will_paginate&#8221; gem.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ gem install will_paginate --version 2.2.2<\/code><\/pre>\n\n\n\n<p>This example installs version 2.2.2 of the gem.&nbsp;<\/p>\n\n\n\n<p>But where does RubyGems look to find the gem? The answer is that the\u00a0<em>gem<\/em>command has a defined set of well-known locations where it will look for gems. This includes locations such as\u00a0<a href=\"http:\/\/www.rubyforge.org\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">RubyForge<\/a>, a free repository of open source Ruby code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ gem uninstall will_paginate<\/code><\/pre>\n\n\n\n<p>This command can be used to uninstall a gem. If there are multiple versions of the gem installed, then the command will prompt for the version to be removed.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ gem update will_paginate<\/code><\/pre>\n\n\n\n<p>Update a gem to the latest version. To update all gems, just use &#8220;gem update.&#8221;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">RubyGems &#8220;Admin&#8221; Features<\/h3>\n\n\n\n<p>You can do lot more with RubyGems than just install and uninstall gems. The&nbsp;<em>gem<\/em>&nbsp;command takes advantage of the information associated with each individual gem to make a variety of other features available to users.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ gem list<\/code><\/pre>\n\n\n\n<p>This command will display a list of all the gems (including version numbers) that are installed locally. Note that you can have multiple versions of the same gem installed on a computer.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ gem query --remote<\/code><\/pre>\n\n\n\n<p>This command will display a list of all the gems that are available remotely from the standard search locations.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ gem spec will_paginate<\/code><\/pre>\n\n\n\n<p>Assuming the will_paginate gem is installed locally, this command will print out detailed information from the gem specification, including version, date created, summary, dependencies, the list of files included in the gem, etc.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ gem environment<\/code><\/pre>\n\n\n\n<p>The&nbsp;<em>gem<\/em>&nbsp;command will also show you detailed information about the RubyGems environment. The above command produced the output in&nbsp;<strong>Listing 1<\/strong>.<\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong>Listing 1: RubyGems Environment<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">RubyGems Environment:\n  - RUBYGEMS VERSION:&nbsp;1.1.0 (1.1.0)\n  - RUBY VERSION:&nbsp;1.8.6 (2008-03-03\n            patchlevel 114) [i686-darwin9.0.0]\n  - INSTALLATION DIRECTORY:&nbsp;\/usr\/local\/lib\/ruby\/gems\/1.8\n  - RUBY EXECUTABLE:&nbsp;\/usr\/local\/bin\/ruby\n  - RUBYGEMS PLATFORMS:\n    - ruby\n    - x86-darwin-9\n  - GEM PATHS:\n     - \/usr\/local\/lib\/ruby\/gems\/1.8\n  - GEM CONFIGURATION:\n     - :update_sources =&gt; true\n     - :verbose =&gt; true\n     - :benchmark =&gt; false\n     - :backtrace =&gt; false\n     - :bulk_threshold =&gt; 1000\n     - :sources =&gt; [\"http:\/\/gems.rubyforge.org\",\n              \"http:\/\/gems.rubyonrails.org\"]\n  - REMOTE SOURCES:\n     - http:\/\/gems.rubyforge.org\n     - http:\/\/gems.rubyonrails.org<\/pre>\n\n\n\n<p>By viewing the output, developers can discover where the Ruby gems are installed on the local computer as well as other information. For example, the output also shows that the default locations where the&nbsp;<em>gem<\/em>&nbsp;command searches for gems are RubyForge and RubyOnRails.org.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">RubyGems Documentation<\/h3>\n\n\n\n<p>There is a comprehensive online User Guide available for RubyGems. It can be found at the URL below:<\/p>\n\n\n\n<p><a href=\"http:\/\/www.rubygems.org\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">http:\/\/www.rubygems.org\/<\/a><\/p>\n\n\n\n<p>I&#8217;d consider the RubyGems User Guide to be excellent if it was completely current. Since it is a little out-of-date in places, I&#8217;ll downgrade it slightly to good, rather than excellent. Despite a few discrepancies between the content and the current state of RubyGems, it&#8217;s still a useful manual.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p>RubyGems has been an extremely successful package. As such, it is now required by Rails 2.0.2 as a dependency. During the boot process, Rails also loads RubyGems, so there&#8217;s no longer a need for developers to use require statements to load RubyGems. This is not reflected in the online documentation, which indicates in many places the need to load RubyGems. Additionally, be aware that RubyGems options may have also changed since the documentation was written.<\/p>\n\n\n\n<p>If developers are working with Ruby 1.8.6 (as most of us still are, at least for production code), RubyGems will have to be installed in each environment. Beginning with Ruby 1.9, RubyGems will be automatically delivered as part of a Ruby installation.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>RubyGems is a package manager for Ruby code. It&#8217;s used to organize reusable code into a deliverable known<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[172,174],"class_list":["post-969","post","type-post","status-publish","format-standard","hentry","category-technology","tag-ruby","tag-rubygems"],"_links":{"self":[{"href":"https:\/\/staging.keenertech.com\/index.php?rest_route=\/wp\/v2\/posts\/969","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/staging.keenertech.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/staging.keenertech.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/staging.keenertech.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/staging.keenertech.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=969"}],"version-history":[{"count":0,"href":"https:\/\/staging.keenertech.com\/index.php?rest_route=\/wp\/v2\/posts\/969\/revisions"}],"wp:attachment":[{"href":"https:\/\/staging.keenertech.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=969"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/staging.keenertech.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=969"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/staging.keenertech.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=969"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}