Rejoining the Tracks

I’ve been away from Ruby and Rails for a long time so before I started working in the environment again, I thought it best to ensure my iMac was up to date with the appropriate software.
If you’re at all serious about using Ruby as a development tool, I sincerely recommend using ‘rvm’ to manage your Ruby versions (as you’ll already know if you’ve read any of my other posts!). Ruby is in continuous development and unlike some languages, it is often the case that the syntax in a new version of Ruby might break code that runs perfectly in an earlier version of the language.
A Ruby Version manager is therefore essential to anybody who works in the Ruby development space for any length of time. rvm is perfect for the job and there are extensive instructions there to cope with installations on OSX, Ubuntu and Windows. I haven’t checked but I’m sure that anybody running a Unix flavor other than Ubuntu would also be able to find help if required.
The first thing I did was update rvm. A quick look on the rvm site told me that the update command was still

rvm get stable

which ran perfectly.
I then checked which Rubies I had installed, noting that Rails 4 (my intended destination) preferred Ruby 2.0.0.
Ruby 2.0.0 was not on my system, so I tried to install it using

rvm install 2.0.0

which immediately produced an error.
Checking the appropriate log file showed the following:

Warning: The Command Line Tools for Xcode don't appear to be installed; most ports will likely fail to build.

(Yes, XCode is required (for its compiler), so install it if you haven’t done so already.)
So, XCode CLI first; that was strange as I knew that I’d downloaded the XCode app from the Apple app store recently.
I ran XCode and realized the problem – downloaded but not installed. On first run, XCode opens the Preferences -> Downloads window from which you can select items to, err, download. The Command Line Tools are not selected by default, so click on them and wait for the CLI to be installed.
I then turned to MacPorts and tried to run

sudo port -v selfupdate

but got an error message. I vaguely remembered having a problem with MacPorts last time I tried to install Ruby 2.0, so I decided to try a clean install.
The MacPorts site has a link for users of various flavors of OSX so I ran the one for Mountain Lion and got an ‘Installation Successful’ message.
Just to make sure, I tried

sudo port -v selfupdate

again, and got the message

---> MacPorts base is already the latest version

The ports tree has been updated. To upgrade your installed ports, you should run
port upgrade outdated

so I ran

port upgrade outdated

as suggested and received a couple of warnings about uninstall being forced for ‘readline’ and ‘freetds’ but got

---> No broken files found.

in the end.

rvm install 2.0.0

then ran without a hitch.
Just to tidy up, I added

source $HOME/.rvm/scripts/rvm

to the end of my .bashrc script. If it is not clear, adding this line to the .bashrc script (which runs every time a Terminal window is opened) enables rvm scripts to run by simply typing e.g.,

rvm help

or

rvm info

Looking at the contents of the
~/.rvm/scripts/rvm folder in Finder shows just how many scripts there are available
Then I ran

rvm alias create default 2.0.0

(which is one of the above mentioned scripts) to set Ruby 2.0.0 as the default Ruby version when I open a Terminal window.
To illustrate, when I open a Terminal window and run either Ruby or irb, I get the following:

martin@polaris:~ ruby -v
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.3.0]
martin@polaris:~ irb
2.0.0-p247 :001 >

One thing that rvm does not do is create the documentation on an install (see http://rvm.io/rubies/docs). This can be checked by attempting to use ri, e.g:

martin@polaris:~ ri Hash
Nothing known about Hash

I fixed this by running

rvm docs generate

However, doing so generated a warning:

Warning! PATH is not properly set up, '/Users/martin/.rvm/gems/ruby-2.0.0-p247/bin' is not at first place,
usually this is caused by shell initialization files - check them for 'PATH=...' entries,
it might also help to re-add RVM to your dotfiles: 'rvm get stable --auto-dotfiles'

so, once the doc build had finished, I ran

rvm get stable --auto-dotfiles

as suggested, which ran successfully and printenv showed that the path had been amended for the terminal window with the Ruby files at first place.
Lastly, the process produced information that might often be ignored, so I thought I’d include it here:

# In case of problems:
# run and read: rvm notes
# read docs: http://rvm.io/
# talk to us: http://webchat.freenode.net/?channels=rvm (http://freenode.net/faq.shtml#plusr)
# read cheatsheet: http://cheat.errtheblog.com/s/rvm
# watch screencast: http://screencasts.org/episodes/how-to-use-rvm
# open a bug report: https://github.com/wayneeseguin/rvm/issues
Advertisements

Rails Date Formats

First published on Fencore Ltd.’s Posterous page on December 3rd, 2010

(Note that the comment about working in Britain is no longer true, as I now reside in the USA.)

I don’t know about you but I get confused when working with dates as the built-in formats don’t work for me.
Ruby has a Date library that implements both the Date and DateTime classes and Rails extends these in its ActiveSupport library. There is also a Time class and, of course, these things cannot exist in isolation from each other.
As I work in Britain, I need to be able to produce dates in British format. I add custom formats to the Rails DATE_FORMATS hash by creating a file in config/initializers with the following contents:

Date::DATE_FORMATS[:british] = "%d/%m/%Y"
Date::DATE_FORMATS[:british_long] = "%d %B, %Y"
Date::DATE_FORMATS[:british_long_ordinal] = lambda { |date|
date.strftime("#{ActiveSupport::Inflector.ordinalize(date.day)} %B, %Y") } # => "25th April, 2007"

According to the documentation (for Rails 3.0.3 look at the ActiveSupport RDocs and then for to_formatted_s(format = :default) in DateTime), the file should be called config/initializers/time_formats.rb but it turns out that is is more of a suggestion than a ‘must do’. I found this out when I called I called it date_formats.rb by mistake; in fact I tried calling it splat.rb and it still worked, so perhaps something a little more generic like date_time_formats.rb would be better.
With that in place, and noting that to_formatted_s is aliased to to_s, you can do things like:

> date = Date.today => Fri, 03 Dec 2010
> date.to_s # this is the default output
=> "2010-12-03"
> date.to_s(:long) # this is pre-defined
=> "December 3, 2010"
> date.to_s(:british)
=> "03/12/2010"
> date.to_s(:british_long)
=> "03 December, 2010"
> date.to_s(:british_long_ordinal)
=> "3rd December, 2010"

You can do a similar thing with times (although I find no need to) and of course, you can define your own formats to satisfy your own local needs.
What about converting from a string to a Date or DateTime object?
Well, the strptime method is part of the Ruby language, so the following works:

> Date.strptime("31/01/2010", "%d/%m/%Y")
=> Sun, 31 Jan 2010 DateTime.strptime("31/01/2010", "%d/%m/%Y")
=> Sun, 31 Jan 2010 00:00:00 +0000

Note that I am using Ruby 1.9.2, although I believe nothing has changed from 1.8.x

Installing ri documentation for rails-3.0.0… File not found: lib and rvm

First published on Fencore Ltd.’s Posterous page on October 13th, 2010

I’ll put the bottom line first: if you’ve tried installing Rails 3 and received an ‘Installing ri documentation for rails-3.0.0… File not found: lib’ error and ended up with no Rdocs, you need to install the rdoc gem first. If you do that, the problem goes way and the install will work perfectly.
I’d been working on a Rails application for some time and had been using it as a learning experience – trying all sorts of new ideas but the wheels fell off last week.
Rails 2.3.8 had been producing lots of deprecation warnings and seeing that 2.3.9 addressed some of those, I thought I’d upgrade. Easy enough – download the new gems (and incidentally clog up my gem repository even more) make a couple of trivial changes to the environment.rb file and try it out. No issues so far.
I needed to move the application to a Windows box and tried freezing the gems into the vendor directory and then copying it across, having installed MySQL and used the Windows Ruby Installer. No luck this time; it’s been a long while since I’ve run Rails on a Windows box but it seems an even less friendly environment than it used to. Apparently the Ruby installer is compiled using a different compiler nowadays and some gems haven’t changed along with it. I needed to install different versions of some gems.
Recognising the existence of rvm, which allows different versions of Ruby to run on the same computer (although you need to use ‘pik’ on a Windows computer), and also of the Bundler gem, which is integral to Rails 3 and allows for better management of gems, I decided to try these out.
Too many changes at once? Darn right. Hindsight is a wonderful thing…
If you follow the instructions carefully, rvm installs easily enough. You can install different versions of Ruby and install different gems for each Ruby version. (rvm calls these gemsets).
In fact, rvm goes further than this – it enables the creation of many gemsets under each Ruby version, allowing you to create gemsets for specific applications. When using a specific gemset, you also have access to a global gemset under that Ruby version. Neat!
Using a file called .rvmrc, you can set things up so that a default Ruby and gemset are chosen when you ‘cd’ to a particular directory.
rvm is an excellent utility and full marks must go to Wayne Seguin and his helpers for developing it. My only carp is about the web documentation, which I really do not like but this is open source right? Live with it or offer to help out!
I did have one huge issue, however. After I had got everything installed to my satisfaction (or so I thought), I tried installing Rails 3.
Now, according to the Rails 3 web site, http://rubyonrails.org/download, you need Ruby, Rubygems and then you can install Rails. If you do and you do not have the rdoc gem installed (which will happen if you use rvm), you will get the error described. Rails will install and work, but you won’t have the documentation locally available. So make sure you have the rdoc gem installed first. (rdoc is also part of Ruby’s standard lib and Rubygems is supposed to install it too – so I guess someone’s not testing enough!)
I have set things up so that the following are available in the global gemset under Ruby-1.8.7; bundler, rake, rdoc, rdoc-data, and syntax. Rake is provided by the rvm setup; the rvm documentation had also stated that rdocs would be installed by default but this wasn’t the case and the rvm web site has had been amended to reflect that.
In my opinion, the Rails site should state that the rdoc gem needs to be installed before Rails (or better yet, make it transparent by including it in the Rails install itself). I’ve tried raisng the issue but so far the response has been that rdoc is not a Rails dependency…