Making your own RubyGems!

Hacker Night 4 puts "Hello again RocHack"

This week, Jeremy gave a talk on the Ruby programming language, and the prominent software distribution system that Ruby uses (RubyGems). Some notes from the presentation:

  1. The guy in the above photo is Matz, creator of Ruby.
  2. He designed the language so that it would be enjoyable to program in.
  3. Specifically, it has a concise, flexible interface, along with a very positive community.
  4. RubyGems hosts Ruby libraries, called gems. Gems are optimized to be super-portable, make versioning code easy, and best of all they are totally free to use (both for downloading/installing code, or uploading gems that you made)!

Gem setup tips

On cycle servers, install into ~/bin, avoiding the need to use sudo. Note that once you do this, you will have to add this folder to your shell’s path. Make this the default when installing gems by running with the two commands down below. This will create a bin folder in your home directory, and set the default the gem installation location to this folder

cd && mkdir bin
echo 'gem: -n~/bin/' >> $HOME/.gemrc

On OSX, the default ruby/gem version needs root access. Some options:

  1. Use sudo every time you want to install a gem (inconvenient).
  2. Setup: echo 'gem: -n/usr/local/bin/' >> $HOME/.gemrc

Try to install a gem, and see if it works:

gem install tv.rb
tv.rb --wavy pride

There should be a rainbow in your terminal. Press Control-C to stop!

Writing a gem

Here is a good example of a simple Ruby gem: (qrush/hola). Initially, there may seem like there are a much of random files. Each has their place and purprose, though. Here is a brief description of each file/folder you may see in a standard gem:

  • Gemspec: gem details (author, license, files)
  • Gemfile: listing gem dependencies (bundle)
  • Rakefile: automate tasks (ruby + make)
  • lib/ where you keep main source code
  • bin/ where you put executables (+x)
  • spec/ or test/ where you put code tests

More help on building your first gem found here.

Thanks to everyone who came out and listened to me rant about Ruby!

– jeremy