Ruby Programming/RubyGems

      RubyGems

      Rubygems is a packaging and "download" system for ruby libraries. Using it is typically as easy as

      $ gem install xxx
      

      where xxx is some gem name you'd like to install.

      More information about rubygems can be had here.

      How to display text to the end user after a gem is installed.

      Gem::Specification.new do |s|
       s.post_install_message  = "Enjoy my gem!"
      end
      

      or "another example":http://github.com/tablatom/rubydoctest/blob/master/rubydoctest.gemspec

      ↑Jump back a section

      How to set the main page for rdocs of a gem

      Gem::Specification.new do |s|
        ...
        s.rdoc_options = ["--title", "EventMachine", "--main", "README", "--line-numbers"]
        s.extra_rdoc_files = ["README", "RELEASE_NOTES", "COPYING", "GNU", "LEGAL", "TODO"]
      end
      

      or "another example":http://github.com/eventmachine/evma_httpserver/blob/master/eventmachine_httpserver.gemspec

      ↑Jump back a section

      How to install different versions of gems depending on which version of ruby the installee is using

      Here's an example: create file ext/mkrf_conf.rb

        require 'rubygems'
        require 'rubygems/command.rb'
        require 'rubygems/dependency_installer.rb' 
        begin
          Gem::Command.build_args = ARGV
          rescue NoMethodError
        end 
        inst = Gem::DependencyInstaller.new
        begin
          if RUBY_VERSION < "1.9"
            inst.install "ruby-debug-base", "~> 0.10.3"
          else
            inst.install "ruby-debug-base19", "~> 0.11.24"
          end
          rescue
            exit(1)
        end 
      
        f = File.open(File.join(File.dirname(__FILE__), "Rakefile"), "w")   # create dummy rakefile to indicate success
        f.write("task :default\n")
        f.close
      

      http://groups.google.com/group/ruby-talk-google/browse_thread/thread/6a03451af53fb853/5d402de5b0da1adf?lnk=raot&pli=1 has a description

      Also add @gemspec.extensions = 'ext/mkrf_conf.rb'@ to your gemspec.

      ↑Jump back a section

      How to install a gem programmatically

      see above.

      ↑Jump back a section

      How to run configure from within an extconf.rb

      Here's an example, from rice's extconf.rb

      require 'rbconfig'
      require 'rubygems'
      require 'ruby/lib/version.rb'
      
      gem_name = "rice-#{Rice::VERSION}"
      prefix_dir = File.join(Gem.default_dir, "gems", gem_name, "ruby", "lib")
      with_ruby = File.join(Config::CONFIG["bindir"], Config::CONFIG["RUBY_INSTALL_NAME"])
      
      other_opts = ""
      env = ""
      
      if RUBY_PLATFORM =~ /darwin10/
        other_opts = "--disable-dependency-tracking"
        env = "ARCHFLAGS='-arch x86_64'"
      end
      
      system "#{env} ./configure --with-ruby=#{with_ruby} --prefix=#{prefix_dir} #{other_opts}"
      
      ↑Jump back a section

      How to use a Rakefile instead of a Makefile

      By default if you list

      gemspec.extensions = 'ext/extconf.rb'
      

      it will expect a Makefile to have been created, and run Make against it, at install time. You can also list @gemspec.extensions = 'ext/mkrf_conf.rb'@ and it will run that, expect a Rakefile to have been created, and run rake against it.

      ↑Jump back a section

      Plugins

      Rubygems now supports plugins, for adding new gem xxx commands, or executing on install and uninstall of gems.

      Known rubygems plugins

      Here's a list of plugins:

      gemcutter gem http://github.com/qrush/gemcutter/blob/master/gem/lib/gemcutter.rb lets you "push" you gem to gemcutter after it's ready

      graph gem http://blog.zenspider.com/2009/04/rubygems-now-has-plugins.html creates a graphviz of your local dependencies

      yard gem has a way to create yard docu after installing a gem (instead of the default rdoc).

      gem_file_conflict_checker gem: warns you when you install two gems whose lib files collide (which can cause serious problems, and is common for gem developers who install their own versions of gems, etc.)

      isit19 http://blog.segment7.net/articles/2009/08/19/rubygems-isit19-1-0 gem

      ↑Jump back a section
      Last modified on 9 July 2010, at 04:57