Ode to The Pragmatic Programmers' Guide Part 1 0

Posted by win
on Wednesday, July 30

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:

  1. Go to http://www.ruby-lang.org
    1. Then choose from the next three options
      1. Click the Download Ruby link and then the Ruby 1.8.7-p22 link – the latest stable version of Ruby
      2. Click the stable snapshot link – a tar’ed and gzip’ed stable version.
      3. Click the Nightly Snapshot link – the edgiest of the edge version (beware may contain issues).

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:

  1. 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

Done.

Request#referer not a defined method in ActionController:TestRequest 0

Posted by win
on Tuesday, July 29

Working on Rails 1.2.3/Ruby 1.8.5 (yes, i know) I recently found out that the referer method is not defined on the ActionController::TestRequest class so will throw an undefined method error during test. To try and solve this I changed

request.referer 

to:

request.referer if defined? request.referer

which DID NOT WORK. Apparently, ‘referer’ is not defined on the normal controller request object as well. I’m guessing the result (which does work) comes from the magical method_missing phenomenon. (Anyone please correct if I’m wrong.) I plan on doing further research later though it may be moot as well will soon be moving to Rails 2/Ruby 1.8.6 – hopefully its not a problem there.

Anyway, to work around it I rewrote

request.referer if defined? request.referer 

to:

request.referer if defined?(request.referr.class)

Hmmmm…