Category Archives: ruby

Rails : The Good Books & Docs

Rails is famous framework, you can code something working with ease, everything you need is there, out of the box. Rails is famous, there are famous people around it, and they are talking about how cool it is. You want to be a cool programmer that earns good money doing something pleasant – learn Rails, you want performance – learn Rails, you want to give good estimates and don’t get stuck in heavy configurations – learn Rails… Or so they say, or so you though…

If you want to do something, do it right. I waited a long time before entering the Ruby world, because I had to figure out what I want as a programmer. I love coding, but I’ve made quite a few wrong decisions for the direction. For example if I look back at my jobs and projects I don’t have anything to show you… (maybe except the fine offline reader that me and my than-colleges made). So I want to code things that people use and I want to be a part of a good community, in which sharing knowledge is not a bad thing. I want to have the opportunity to create things that will be used by my fellow programmers or the common people. This is why I chose my last job as ruby developer over the JEE opportunities I had. I saw the language, platform and community as the something I needed at the moment. Then I’ve quit my last job to pursue different goals with Ruby, Rails and Sinatra

Why I’m telling you this, you came here to read about the books that would help you learn Rails, not to listen to the story of a nobody. But in this post I’ll share my thought about some books, so why not share my experience that lead me to them. Think about what you want before learning Rails… If you want to work in a corporate company with big budgets, its not for you. If you want to earn money without giving a thought, it is not for you (try PHP).

If you want to learn Rails, first learn Ruby. Every language has its secrets that you need to know. The language is a tool, you should learn how to use it. The framework is an extension of this tool. So read my post about the good Ruby books and docs if you want.

Now, lets begin:

  1. Agile Web Development with Rails – In this book you will develop your first Rails application on iterations. After that you will learn about the different Rails modules in depth. Very good and easy to read, this is your entrance to Rails. It is written by Sam Ruby and published by The Pragmatic Bookshelf. At the time of this writing there is a beta version of the fifth edition of the book for Rails 4.

  2. The Rails 3 Way – It is both a Rails reference and bag full of advises by professionals. It shows you the way of Rails and good examples how to bend it but not break it. It is written by Obie Fernandez and published by Addison-Wesley. ‘The Rails 4 Way’ is on its way :)
  3. http://railscasts.com – Watch this video casts and there will be no new things in the Rails world for you. By Ryan Bates.
  4. http://railsapps.github.io/tutorial-rails-devise-rspec-cucumber.html – This is the best tutorial about Rails with Rspec and Cucumber I can think of…

I think these four are more than enough. The first one will teach you how to implement your first application and understand how it works, the second one will get you deeper in Rails and how to use it and the third one will show you the whole gem universe around Rails… The fourth one is just good :)

Ruby : The Good Books & Docs

If you want to learn rails, you should learn ruby. If you want to learn ruby or to get more proficient in ruby you should read some books/articles/documentation/sources.
I read quite a few things about ruby, some of these links and books were given to me by my friends and colleges, I want to share them with you…

  1. I recommend “Programming Ruby 1.9 & 2.0” by Dave Thomas, with Chad Fowler and Andy Hunt. This a book from The Pragmatic Bookshelf. The best book about Ruby. It is a complete reference of Ruby and its standard libraries. This is a must. I know that most of the books about programming languages begins with definitions of the key words, conditionals and loops, but this is not the case with this one.
    + Pleasant for reading.
    + Good examples.
    + Everything in one.
    + Full reference for the standard libraries that come with Ruby.
  2. If you want to get under the surface of Ruby and to have the power to write your own DSLs, the book for you is “Metaprogramming Ruby: Program Like the Ruby Pros”, by Paolo Perrotta. Again the book is published by The Pragmatic Bookshelf. It will teach you the inner doings of the Ruby classes, modules and methods. It will be easier to you to get into how things are implemented in frameworks like Rails.
    + Gives you the information you need on iterations.
    + Cool story :)
    + Advanced but enjoyable.
  3. This article – http://blog.jcoglan.com/2013/05/08/how-ruby-method-dispatch-works is on the same topic as the above book. If you don’t like books, I recommend it, if you like books and read the above one, I still recommend it. It is a good article and James Coglan, the author re-implemented Ruby’s method dispatch in Javascript and in Ruby. This link was given to me by Gudata.
  4. I just love The Pragmatic Bookshelf’s books. I wanted to give you links to books from another  publisher here, but I can’t. Everything you need is on their bookshelf.  “Working with Unix Processes” and “Working with TCP Sockets” are two books by Jesse Storimer. They are very practical and some very important knowledge is provided to you in the form of Ruby code.
  5. Very important place is http://ruby-doc.org. It is self-explanatory.
  6. Another link of interest is https://www.ruby-toolbox.com. It compares the shares of use of different frameworks and libraries grouped by their purpose.
  7. http://www.ruby-lang.org/en

This post is only useful links, so no useful links here… I think it is a good idea to write such posts. There will be post about rails books and links and maybe REST, Vim, Ruby Web… Who knows.

Ruby Bundler : A Source Full Of Gems

TL;DR : Bundler tracks your application’s code and the gems it needs to run, so that the application will always have the exact gems (and versions) that it needs to run. Add the Gemfile.lock generated by Bundler to version control. Bundler’s home is http://gembundler.com.

We know how to install and manage our rubies, and we know how to install and store our gems. The one thing left is how to manage them. The tool for that is Bundler.
It gives you the power to manage your gems per-project. For example Rails uses Bundler to manage the libraries used by an application. You can put your gem configurations under source control and that way all the developers will work in the same environment, with the same versions of the gems. If you remember my article about portable Vim, the idea for the VundleFile is influenced by Bundler’s Gemfiles.

Now is time to install Bundler:

gem install bundler

If we use Rbenv we should rehash… Now you can create a sample Ruby project for example something like:

mkdir -p ~/development/ruby/project
cd development/ruby/project
mkdir lib
mkdir spec

Your source code will be in ‘lib’, your specs will be in ‘spec’ (If you don’t know what is a spec, you will be very happy to find out here).

If we want Bundler to manage the gems for our application we can create a file called Gemfile or execute

bundle init

which will generate it for us.

The Gemfile lists the gems used by our projects. A Gemfile contains one or more sources:

source :rubygems
source 'http://rubygems.org'
source :rubyforge
source 'http://gems.rubyforge.org'

These sources point to the gem servers which will be used to download the gems for the project. If you ran ‘bundle init’, you already have the rubygems.org source.

If you have at least one source, you can add the gems needed by your project. You can:

  1. Depend on gems from the source servers, listing only their names.

    gem 'rspec'
  2. Depend on given gem versions or version ranges.
    See http://docs.rubygems.org/read/chapter/16#page74 or more information. You should read about ‘~>’ if it is new for you.

    gem 'rails', '4.0.0.rc1'
    gem 'rack',  '>=1.0'
    gem 'thin',  '~>1.1'
  3. Depend on a gems located in a Git repositories. You can specify revisions, tags and branches.
    gem 'nokogiri', :git => 'git://github.com/tenderlove/nokogiri.git'
  4. Depend on gems downloaded locally or written by you.
    gem 'nokogiri', :path => 'gems/nokogiri'

You can specify the Ruby versions your project is compatible with:

ruby '1.9.3'

And you can create gem groups. For example:

# These gems are in the :default group
gem 'nokogiri'
gem 'sinatra'

gem 'wirble', :group => :development

group :test do
  gem 'faker'
  gem 'rspec'
end

group :test, :development do
  gem 'capybara'
  gem 'rspec-rails'
endgem 'cucumber', :group => [:cucumber, :test]

This way, for example, gems needed by the specs will be required only when you run Rspec, gems needed by the production will be required only on the production server, etc…

For more about Gemfiles – http://gembundler.com/v1.3/gemfile.html.

Now if you run:

bundle install

or just:

bundle

All the gems listed in the Gemfile will be downloaded and will be available to your project. A special Gemfile.lock will be generated. This Gemfile.lock is very important. You must add it to version control! Be aware that Bundler downloads the gems listed in the Gemfile and their dependencies. By default they are downloaded in your current ruby installation folder’s ‘gems‘ sub-folder.
The Gemfile.lock contains the dependencies and the exact versions of the downloaded gems.
Next time you run the ‘bundle install‘ command this file will be used by Bundler to decide which versions should be downloaded.
Think about it… When you wrote your code, you listed rack as gem without specifying its version. The version of rack was 1.0 at the time, but now it is 2.3 (for example). The current version’s code is very different and your code is not compatible with it. But if you checkout your project somewhere and the Gemfile.lock is in it, Bundler will download rack version 1.0, because this is the version in the Gemfile.lock, the version that was the newest when you first run ‘bundle install‘.
If you wan to update the Gemfile.lock to include the newest versions, there is ‘bundle update‘ command. I’m not going to talk about it now, but do not update Gemfile.lock manually!

So this is Bundler, you and your collaborators will work with the same ruby version and the same gems and gem versions. This way the project is set-upped to work everywhere. When your friends checkout the project and run ‘bundle install‘, everything will be downloaded and configured.

I can continue talking about installing gems contained only in given group, or installing gems in your project’s path, but I think the post is long enough.

You can require your all the gems from your Gemfile or different groups using:

Bundler.require(:default, [:<group_name>, [<more_groups>]])

There is a Bundle.setup method too which adds your gems to the load paths and then you can require them manually. But more about that some rainy day.

Useful Links:

  1. Bundler’s site – http://gembundler.com
  2. Bundle.setup vs Bundle.require, a topic I didn’t cover – http://anti-pattern.com/bundler-setup-vs-bundler-require
  3. About Bundle.install – http://gembundler.com/v1.3/man/bundle-install.1.html
  4. How to use Bundle.update – http://ilikestuffblog.com/2012/07/01/you-should-update-one-gem-at-a-time-with-bundler-heres-how

Ruby Gems : Lucy In The Sky With Gems

Python has its eggs, Java has its jars and ruby has its gems. So a gem is a ruby package, containing some library or executable program. In the previous two chapters about RVM and Rbenv I installed rails using the gem command. Since ruby 1.9, Ruby Gems comes with ruby. So, how to use it?

  1. You can list all the available gems, hosted on RubyGems.org with:

    gem query --remote (gem q -r)
    or
    gem list -r


  2. You can search for remote gems with:
    gem query --remote --name-matches <name>
    or
    gem search -r <name>
    or
    gem search <name>
    or
    gem sea <name>
  3. You can install a gem using:
    gem install --remote <name>
    or
    gem install <name>
    or
    gem ins <name>
  4. You can install a given version of a gem using:
    gem install --remote <name> --version <version>
    or
    gem install <name> --version <version>
    or
    gem ins <name> --version <version>
    or like that
    gem install <name>-<version>

    For versions you can use  operators :

    gem install <name> --version "= 0.1.3"  
    
    All operators:
      =  Equals version
      != Not equal to version
      >  Greater than version
      <  Less than version
      >= Greater than or equal to
      <= Less than or equal to
      ~> Approximately greater than 

    To see all available versions of a gems:

    gem search -a <name>
    or
    gem list -r -a
  5. To see the locally installed gems:
    gem list (-l)
    or
    gem query -l (--local)

    To search in the locally installed gems for given gem:

    gem search -l <name>

    A detailed information for an installed gem:

    gem specification <name>

    This can be executed with the ‘-r’ option for remote gem.
    A cool feature is to run

    gem server

    And to use your browser, opening http://localhost:8808 to browse detailed information for your installed gems.

  6. To uninstall a gem:
    gem uninstall <name>
  7. You can install gems with options like ‘–no-rdoc or ‘no-ri’, see all available using:
    gem help install

    You can put a .gemrc file in your home with some commands and settings to be used with Ruby Gems. The file should look like this:

      gem:  --local --no-rdoc --run-tests
      gempath:
        - /usr/share/rubygems
        - /home/meddle/.stuff

    Read for more here http://docs.rubygems.org/read/chapter/11

I think this is a good manual for the tool, we will use it a lot in the up-coming posts.

Useful Links:

  1. Ruby Gems site – http://rubygems.org
  2. Ruby Gems docs – http://docs.rubygems.org
  3. Manual at http://www.ruby-lang.org – http://www.ruby-lang.org/en/libraries

Rbenv : Careful With That Rubies, Rbenv

This post is about the alternative of RVM (which I presented to you in my last post) – Rbenv. I like the open source world, there are always alternatives…

You’ll ask : “What’s the difference?”. Hmm there are differences:
<<< RVM is tool, designed to bring you listing of all available rubies in the net, downloading and compiling them, switching between them, gemsets.
>>> Rbenv is tool for switching between ruby versions. You download and install the rubies, you decide what tool for dividing the gems to sets should be used.
<<< RVM overrides the ‘cd’ command, in other words when you change directory, the command also ‘looks’ for .rvmrc file in it and executes it. This way you can heave per-project ruby versions.
>>> Rbenv changes the executables in you downloaded gems so if you execute ‘rails’ it looks for .rbenv_version file in the current directory (or in its parents if there is no such file) and uses it to decide the version.
<<< RVM is all in one.
>>> Rbenv has a plugin system and can be set up to be on par with RVM‘s functionality.

If you ask me, which is better, I’ll sincerely answer you – it depends. The important thing is – you should use a tool that works for you, helps you and doesn’t get in your way. For now I prefer Rbenv, because I like the idea of upgrating/downgrating thing through plugins…

Now, this will be a tutorial similar to the one in the post for RVM, I’ll teach you to do the same stuff but with Rbenv.

First of all if you have RVM, remove it the two tools don’t work well together:

rvm implode

Now let us install Rbenv:

git clone git://github.com/sstephenson/rbenv.git ~/.rbenv

This downloads Rbenv in your home’s .rbenv directory.

echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile

This prepends the rbenv commands to your PATH. For Ubuntu and Mint use .profile, for zsh – .zshrc.

echo 'eval "$(rbenv init -)"' >> ~/.bash_profile

This enables shims and auto-completion.Again for Ubuntu and Mint use .profile, for zsh – . You can read more about shims at the Rbenv's github.

exec $SHELL -l

This will restart your shell and the PATH changes will take effect.

We can list the available ruby versions with this:

rbenv versions

We don’s have rubies. So how to install one? I said that Rbenv can only switch between already installed rubies, so you can download ruby binary or source (and compile it) and put it in .rbenv/versions, for example ‘~/.rbenv/versions/1.9.3-p429’. But there is another way. I mentioned earlier, that Rbenv has a plugin system, so we will install a plugin called ruby-build which purpose is to download and install/uninstall rubies.

mkdir -p ~/.rbenv/plugins
cd ~/.rbenv/plugins
git clone https://github.com/sstephenson/ruby-build.git

This is it. To install a plugin, all you have to do is to put it in  ~/.rbenv/plugins. Now lets list the known rubies:

rbenv install --list

Cool. Now to install ruby 1.9.3, patch 429:

rbenv install 1.9.3-p429
rbenv rehash

The second line is important. When you download rubies or executable gems, you will have to run it to update the shim files. Now we have ruby 193-p429. Lets select it globally:

rbenv global 1.9.3-p429

This is the default ruby as of now. You can view it with ‘rbenv global’ or ‘rbenv version’.
Let us continue following my RVM post and to install ruby 2.0.0 with rails:

rbenv install 2.0.0-p195
rbenv rehash
rbenv global 2.0.0-p195
gem install rails
rbenv rehash

Everything is done, now if you run ‘rails -v’ you will have Rails 3.2.13 installed, but if you switch the global ruby with ‘rbenv global 1.9.3-p429’, I won’t have rails installed.

Gemsets… You don’t have them. If you want something similar, you will have to install a plugin.

mkdir -p ~/.rbenv/plugins
cd ~/.rbenv/plugins
git clone git://github.com/jamis/rbenv-gemset.git

So we have the gemset plugin. The idea is that you can include gemsets in a project. Create somewhere a directory for a rails 4 project. Go to this directory and execute:

echo rails4 > .rbenv-gemsets

Now for this folder the gemset is rails4, if you want to use this gemset in another project just navigate to its root folder and execute the above command. That’s it. So now if we are in a folder that is modified this way, we can install rails 4:

gem install rails --version 4.0.0.rc1 --no-ri --no-rdoc

Try it, if you are in the folder the rails version is 4, if you navigate out of it is 3.2. Now the same useful things as in the post for RVM:

  1. To delete a gemset, just delete the .rbenv-gemsets folder.
  2. To use the system ruby just delete the global version file – ~/.rbenv/version or run
    ‘rbenv global system’. If you have problems look at RBENV_VERSION environment variable, you may have to unset it.
  3. To update Rbenv just navigate to ~/.rbenv and pull the newest version through git.
  4. To delete Rbenv just delete the ~/.rbenv  folder and clear your shell’s profile file from Rbenv specific stuff.

Useful Links:

  1. Rbenv site – http://rbenv.org
  2. Ruby Toolbox’s page about Ruby Version Managers – https://www.ruby-toolbox.com/categories/ruby_version_management
  3. Cool post about RVM and Rbenv – http://jonathan-jackson.net/rvm-and-rbenv
  4. Rbenv plugins – https://github.com/sstephenson/rbenv/wiki/Plugins

 

RVM : Set The Controls For The Heart Of Ruby

Many ruby library versions work better with given ruby version. This includes rails versions as well. Another thing is that there are more than one implementation of ruby, for example jruby. If you want to easily switch between implementations and versions you will have to use a dedicated tool. It is easy to install ruby using homebrew or apt-get, but it is not so easy to manage its versions and implementations. Enter RVM – the Ruby Version Manager.

We will begin by installing RVM using our favorite HTTP client – cURL.

curl -L https://get.rvm.io | bash
rvm requirements

Now if you use mac/linux/unix you will have RVM installed. If you use windows (shame on you, and you call yourself a developer) you can’t use RVM (maybe you can using cygwin, who knows), try using PIK instead. By the way there are quite a few installation options on the RVM home site.
The second line may output some instruction you must do before using RVM.

Now we can list our ruby versions using:

rvm list

RVM will tell us that we have no rubies installed. We can check out the available rubies running:

rvm list known

We will see MRI rubies (these are the Matz’s rubies – the C rubies), Jrubies, IronRubies, etc… We are able to install not only different versions but different patches of versions as well. I’m going to install the latest path of ruby 1.93.

rvm install ruby-1.9.3

This will download the ruby source and will compile it. Now if we type

rvm list

We will see our new ruby. With

rvm use ruby-1.9.3

we are already using it. The list command has a legend. We can see that there is something called default ruby. This is the ruby that will be used in all your sessions. Let we set 1.9.3 as our default ruby:

rvm use --default 1.9.3

We have only one ruby now… Let us install one more, ruby 2.0.0, patch 195:

echo progress-bar >> ~/.curlrc
rvm install ruby-2.0.0-p195 -j 3

The first command will add a progress bar when your are downloading a ruby. My machine has four cores and the -j options tells RVM to compile the newly downloaded ruby using 3 of them.
Now we have two ruby versions and we can see them in detail running the ‘rvm list’ command, we can switch between them using the ‘rvm use <ruby-version[-patch]>’ command. The important thing is that if we install gems (these are ruby libraries), they will be installed only for the current ruby. For example let us install rails for ruby 2.0.0:

rvm use 2.0.0
gem install rails

With the first command I want to show you that ‘use’ works only with versions as well (for MRI rubies). The second command installs the rails gem for the current ruby2.0.0. This will install many gems, because Rails has many dependencies (you can read some manga, while waiting).

Now when I run

rails -v

the output is Rails 3.2.13. But if I switch rubies and run it again:

rvm use 1.9.3
rails -v

I have an error. Rails is not installed. So I installed rails only for ruby 2.0.0 and if I want to install rails for 1.9.3, I will have to select ruby 1.9.3 and install it there.

But what if I want to live on the edge! What if I want to use ruby 2.0.0 with rails 4. I don’t want to remove rails 3.2 from ruby 2.0.0, because I am working on some project with it. How can I switch between different versions of one library for a given ruby, how can I have different set of libraries, different gem sets. RVM has gemsets. These are independent spaces filled with gems, they are unlimited for any ruby version. We can see all gemsets for all ruby versions by running:

rvm list gemsets

The output for me is:

rvm gemsets

=> ruby-1.9.3-p429 [ x86_64 ]
   ruby-1.9.3-p429@global [ x86_64 ]
   ruby-2.0.0-p195 [ x86_64 ]
   ruby-2.0.0-p195@global [ x86_64 ]

All ruby versions have a default gemset, the one that is used without specifying gemsets and a global gemset, all the gems in the global gemset are accessible by all other gemsets for the given ruby. Now we are going to create a new gemset for ruby 2.0.0-p195 called rails4. It will contain the Rails 4 gem. Run the following:

rvm use 2.0.0
rvm gemset create rails4
rvm gemset use rails4
rvm gemset list

Now we are using ruby 2.0.0, we’ve created a new gemset called rais4 with the second line, with the third line we told RVM to use the newly created gemset and the last command lists the gemsets of the current ruby. The output of the last command looks like this:

gemsets for ruby-2.0.0-p195 (found in /home/meddle/.rvm/gems/ruby-2.0.0-p195)
   (default)
   global
=> rails4

We can also choose to use a given ruby with a given gemset using ‘rvm use <ruby>@<gemset>’.
OK we are ready to install Rails 4:

gem install rails --version 4.0.0.beta1 --no-ri --no-rdoc

OK, I showed you how to use RVM, how to manage rubies and gemsets. All that is left is to show you a few more useful commands and to call it a post.

  1. You can remove a gemsets using:
    rvm gemset delete <gemset>
  2. You can use the ruby installed on your system (without RVM through for example apt-get) using:
    rvm system
  3. You can update RVM using:
    rvm get stable
  4. You can remove RVM and all the rubies installed with it using:
     rvm implode

So this is it. And one more thing… From this post on, I’m going to have an useful links section at the end of all posts:

Useful Links:

  1. RVM site – https://rvm.io
  2. Ruby Toolbox’s page about Ruby Version Managers – https://www.ruby-toolbox.com/categories/ruby_version_management
  3. Cool post about RVM and Rbenv – http://jonathan-jackson.net/rvm-and-rbenv
  4. My post about cURL link

Now today is 29-th may, the birthday of my dear friend and ex-colleague Icata Banchev, so Happy Birtchday ICO!