Ruby Tools
TODO
You will be writing a lot of Ruby code. It is beyond scope of this book to teach Ruby, so if you are not familiar with it, you should read a book or two about it. You should also get familiar with Ruby tools like IRB (Interactive Ruby Shell), RubyGems, Bundler, RDoc, RVM (Ruby Version Manager) and The Ruby Toolbox.
IRB (Interactive Ruby Shell)
|
You do need internet access if you want to follow examples in this chapter. All examples in this chapter are tried on Mac OS X 10.8.5 and Ruby 2.1.1p76 but everything should work on all supported platforms. |
If you are familiar with Ruby, I am sure you already think IRB (Interactive Ruby Shell) is one of the greatest tools for learning a new Ruby library. If you are new to Ruby, you are probably thinking: What is this IRB thing? IRB is Interactive Ruby Shell. We will not get into technical details here, all you need to know is that it is a shell that executes Ruby commands. Every command executes immediately and returns something. Sometimes you will be interested in the return value, the rest of the time you can ignore it.
|
IRB is a command-line interface (CLI) tool. If you are not familiar with CLI, see Command-line interface chapter. |
To start IRB, type irb in command line. On Mac and Linux, it should look like this:
1 $ irb
2 2.1.1 :001 >
On Windows, it should look like this:
1 >irb
2 DL is deprecated, please use Fiddle
3 irb(main):001:0>
If you see something slightly different, do now worry, if the examples from this chapter work fine. Let’s take a look at the output. Mac and Linux have 2.1.1 :001 > as prompt and Windows has irb(main):001:0>. On Mac/Linux, 2.1.1 part is Ruby version. Windows does not display it. Windows has irb(main), so you do not forget you are using IRB. Windows also displays DL is deprecated, please use Fiddle warning message. Feel free to ignore it.
:001 on Mac/Linux and :001:0 on Windows is line number. If you just press enter/return (depending on your keyboard), you should see something like this.
Mac/Linux:
1 $ irb
2 2.1.1 :001 >
3 2.1.1 :002 >
Windows:
1 >irb
2 DL is deprecated, please use Fiddle
3 irb(main):001:0>
4 irb(main):002:0*
Since the part before the > or * is not important for us at the moment, we will ignore it in the rest of the book. To differentiate between regular CLI and IRB, the book will use $ for the regular CLI and > for IRB. Let me explain that with a couple of examples.
Executing ls command in Mac/Linux CLI:
1 $ ls
2 Book.txt Gemfile.lock README.md VERSION images lib
3 misc Gemfile LICENSE.md Subset.txt epub_images
4 installation main old
Executing 1+1 in IRB:
1 $ irb
2
3 > 1+1
4 => 2
The above IRB example shows that Ruby knows how to calculate sum of two numbers. You can also ask it to output something:
1 > p "wow much output so hip"
2 "wow much output so hip"
3 => "wow much output so hip"
After outputting the string, Ruby returned it. You can ignore it for now. All you need to know is that it did not complain. Let’s try something that will fail:
1 > doge
2 NameError: undefined local variable or method
3 `doge' for main:Object
4 from (irb):7
5 from /Users/z/.rvm/rubies/ruby-2.1.1/bin/irb:11:in
6 `<main>'
Looks like Ruby does not know about Doge meme. Important thing here is to notice how an error message looks like. Let’s do something useful now, like nicely formatting output. First, we will create a doge variable:
1 > doge = {dogecoin: "To the moon!",
2 hello: "This is Doge", wow: "much addon",
3 so: "internet"}
4 => {:dogecoin=>"To the moon!",
5 :hello=>"This is Doge", :wow=>"much addon",
6 :so=>"internet"}
Then we will try to output is nicely with pp library:
1 > pp doge
2 NoMethodError: undefined method `pp' for
3 main:Object
4 from (irb):9
5 from ...irb:11:in `<main>'
Looks like Ruby does not know about pp too. All you need to do to fix the problem is to require the library:
1 > require "pp"
2 => true
Let’s output doge, finally:
1 > pp doge
2 {:dogecoin=>"To the moon!",
3 :hello=>"This is Doge",
4 :wow=>"much addon",
5 :so=>"internet"}
6 => {:dogecoin=>"To the moon!",
7 :hello=>"This is Doge", :wow=>"much addon",
8 :so=>"internet"}
PP is a pretty-printer for Ruby objects. It is really useful for outputting big objects.
|
IRB is not the only Ruby shell. There is irb Alternatives category at The Ruby Toolbox. The most popular alternative tool is Pry, but there is another one called ripl. |
RVM (Ruby Version Manager)
On Mac and Linux you can install and use multiple versions of Ruby at the same time using RVM (Ruby Version Manager).
Install RVM:
1 $ \curl -sSL https://get.rvm.io | bash -s stable
2 ...
Open new Terminal tab or window. That is important. RVM might not work properly if you do not open new tab/window after installation. Finally, install a recent version of Ruby:
1 $ rvm install 2.1.1
2 ...
Ask RVM which versions for Ruby are installed:
1 $ rvm list
2
3 rvm rubies
4
5 ruby-2.1.0 [ i686 ]
6 ruby-2.1.1 [ i686 ]
Terminal will not be aware of the Ruby installed via RVM until you explicitly tell it to use it. Try asking the Terminal for Ruby version:
1 $ ruby -v
2 The program 'ruby' can be found in the following
3 packages:
4 * ruby1.8
5 * ruby1.9.1
See, Terminal does not think Ruby is installed. Now, tell RVM you want to use the latest version:
1 $ rvm use ruby-2.1.1
2 Using /home/z/.rvm/gems/ruby-2.1.1
Ask Terminal again for Ruby version:
1 $ ruby -v
2 ruby 2.1.1p76 (2014-02-24 revision 45161)
3 [i686-linux]
Now it knows about Ruby installed via RVM. You can also ask RVM which Ruby is it using currently:
1 $ rvm list
2
3 rvm rubies
4
5 ruby-2.1.0 [ i686 ]
6 => ruby-2.1.1 [ i686 ]
7
8 # Default ruby not set. Try 'rvm alias create
9 default <ruby>'.
10
11 # => - current
12 # =* - current && default
13 # * - default
Currently used Ruby (ruby-2.1.1) is marked with the arrow (=>).
The Ruby Toolbox
The Ruby Toolbox is a really useful site where you can find and compare a lot of libraries. Libraries are grouped in categories. Whatewer you are looking for, it is probably listed there. Some of categories and tools are: IRB alternatives (like Pry), PDF processing (like PDF::Reader), image processing (like Chunky PNG), log analysis (like logstash), dependency management (like Bundler), acceptance test frameworks (like Cucumber), browser testing (like Selenium), distributed testing (like ParallelTests) or unit test frameworks (like RSpec).
