Tag Archives: Rails

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 :)

Advertisements

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