Though I graduated with a Computer Science degree and started my career programming Java for a stint, I feel I didn’t really start coding the way I wanted until I read a book that most Rubyists regard as the book that started it all. The “pickaxe” it is now called has been a staple at my work desk for these past months and in honor of the help it has given me I’ve decided to try and condense the more than 400 pages (the rest of the pages are reference) of programming prose to no more than 26 blog posts.
Why am I doing this? Am I bored? Is this just so I have more to write on m blog? Will it even be useful?
Not sure. Probably. Sadly, maybe. Who knows.
Though I will still try and throw in unrelated posts (and maybe some additions to the summaries ) of my more original thoughts and ideas, I will begin with Chapter 1 – though elementary I’d like to be comprehensive at least.
And here we go…
Programming Ruby – The Pragmatic Programmers’ Guide
Chapter 1 Summary
Installing Ruby
Nowadays Ruby comes preinstalled on many Linux distros and Mac OS X systems. If Ruby is not installed or you’d like to have it handled through Macports check out my earlier blog post. To find out if you need to upgrade ruby use the
ruby -v
to find the version of Ruby you’re running.
Binary Distributions
Binary distros work fine but you are limited to the versions that are available. Below you can find references for RPM-based systems, Debian dpkg-based systems and MacPorts (courtesy of my own link).
- RPM – search http://www.rpmfind.net entering “ruby” as a search term
- Debian – use apt-cache to search for ruby packages
apt-cache search ruby
then use
apt-get install <rubyandversion>
to install it.
Building Ruby from Source
Using this method you take on all the responsibility for managing the build and installation process. If you’d dare follow the steps below:
- Go to http://www.ruby-lang.org
- Then choose from the next three options
- Click the Download Ruby link and then the Ruby 1.8.7-p22 link – the latest stable version of Ruby
- Click the stable snapshot link – a tar’ed and gzip’ed stable version.
- Click the Nightly Snapshot link – the edgiest of the edge version (beware may contain issues).
- Then choose from the next three options
Once downloaded untar the files using the following command
tar xzf <file>.tar.gz
This will install the Ruby source tree in the ruby/ subdirectory. Find the README file in that directory for the installation procedure.
Running Ruby
Ruby can be run in two ways:
- Interactively using
ruby
at a prompt followed by ruby commands or the more preferred method
irb
which allows you to see what you’re typing better and also allows for the loading of the second type (Ruby programs) using
load "src/examples/ruby_file.rb"
after typing ‘irb’ to start the Interactive Ruby.
2. In Ruby Programs
Obviously, Ruby can be run from a file by running the Ruby interpreter with the script name as an argument
ruby example.rb
The Unix shebang notation placed on the first line of an executable file will also work.
#! /usr/local/bin/ruby -w
Note: to find what exactly to put after the shebang (#!) do a
which ruby
to find where ruby is located in your system and copy and paste that after the shebang.
Ruby Documentation: RDoc and ri
The Ruby libraries are documented internally using a system called RDoc. ri is a command-line tool used to view documentation created in the RDoc system.
To find the documentation for a particular class use
ri <Classname>
To look at a single method for a class use
ri <Classname>.<method>
For more help using the ri tool use
ri --help
