June 9th, 2009
After a tough day in the office you want to catch up on the news, so you look at the ticker on a TV channel or tune in your car radio. Other days you’ll want to sit down with a meaty broadsheet and really take in the detail of what lies behind the headlines. This is a book about Ruby which which triumphs at walking the line between these two styles. The Well Grounded Rubyist aims to appeal to a developer that has been exposed to some Ruby coding and take their knowledge to the next level. And it succeeds brilliantly.
This is not a book about Rails or any other web frameworks; purely Ruby. Though much of the material will also apply to the 1.8.x series of Ruby, this is a book about the 1.9 version of the Ruby language. It’s broken into three parts - Ruby foundations, Built-in classes and modules and finally Ruby dynamics. But don’t let the title of Ruby foundations fool you for part one - this is not some remedial rush through the basics of Ruby in six chapters. Rather, after a couple of warm-up chapters, it moves quickly to clarify the key aspects of how classes and module inter-relate, as well things such as crystallising what ’self’ really means in different contexts in a Ruby program. The author sets out his stall early - what makes Ruby different from other languages is it’s focus on objects rather than classes. Everything else stems from this and by the end of the section you feel like you have an understanding of Ruby’s design and focus.
Part two of the book is Built-in classes and modules. Now that you know what makes Ruby tick, it’s time to get seeped in all aspects of the core library that ships with it. One of the problems when learning a language is that becoming familiar with all methods of a particular core class is a tedious task. It’s much more interesting to learn about concepts such as meta programming than memorising lists of methods by rote. But if you don’t take the time to familiarise yourself with the dusty corners of a language’s API then you’re less likely to think of those handy methods when a problem they would elegantly solve presents itself. At this point the book shifts gear to a more reference style of text. However, it still gives the reader an interesting story to follow as it documents arrays, hashes and other classes - throwing in the occasional golden nugget of information that will be a valuable additional to the toolbox of even experienced Rubyists. One side effect of the change in style is that this section is probably the most accessible to beginners. Again it’s broken down into six chapters. In addition to collections, it also covers topics such as regular expressions and file handling. Each topic takes a zero-to-hero approach meaning that you can bring little regular expression knowledge to the table yet still walk away learning an immense amount about the subject.
The final part of the book, Ruby dynamics, returns to the book’s roots from part one - a focus on imparting a deep knowledge of Ruby’s design. Before you even pick up this book you have an inkling that procs and lamdas are going to make a guest appearance at this late stage. And they do not disappoint. Extending the behaviour of objects takes centre-stage and meta-programming based techniques move quickly to the fore. Any block/proc/lamda confusion you may have will be a distant memory by the time you finish this section. Threading is also covered here - though a detailed discussion of 1.9’s new native OS threads vs green threads is left to one side to focus on the usage of threads regardless of which underlying type you use. Lots of material is also provided on querying objects; which is not only useful for program design but also invaluable as a debugging aid. The book really shines in this section because a lot of other texts make the mistake of going into ’super-boffin’ mode at this point, leaving the reader lost, whereas the author here continues to provide patience and context to get you round that final lap on you way to becoming a Well Grounded Rubyist!
This book cannot be all things to all people. Because it is catering for a wide range of intermediate to advanced Rubyists, it will feel like it’s moving a little too slowly for some. By the author’s own admission, this is in order to make it accessible to a wider audience and no doubt it will make it easier for developers of all levels to digest - your humble reviewer very much included! Ruby first-timers would be best off having some straight-to-the-point tutorials or entry-level text to hand in order to get some instant gratification - as part one of the book, by it’s very nature, is a little more abstract than a complete beginner would expect. But all in all, this book is a great way to learn just how Ruby crams so much expressiveness into such a simple clean framework. Whenever I read a book like this I keep a list of new things learned along the way. For The Well Grounded Rubyist it is a very long list! Well done to David A. Black and Manning for producing a book that fills those gaps in many Rubyists’ understanding of the language while at the same time delivering an absorbing readable book that would sit proudly on any Ruby programmer’s bookshelf.
Posted in Geek, Opinion Piece, Programming, RubyIreland, Rubyonrails | No Comments »
May 11th, 2009
An unusual blog post this in that I have such little value to add other than pointing you at two great links. The first is a wonderfully useful post on tweaking Phusion Passenger when running some Rails apps on a 256MB Slicehost VPS and the other link is a no-nonsense explanation of tweaking Apache config options to cut down on your swap memory usage. Thanks so much to these folks!
Posted in Geek, Linux, Programming, RubyIreland, Rubyonrails | No Comments »
March 27th, 2009
“What?” you say, “that’s a lot of Ubuntu going on! Why would you want to do such a thing?” The answer - for testing applications, replicating environments or perhaps running a few servers on one box.
This guide should work pretty well for any Linux distro, though you may need to replace the aptitude commands with the package manager of your choice. Here we’ll install a Hardy Heron guest inside an Intrepid Ibex host. Afterwards, we’ll also use dselect with apt in order to clone the packages on a machine we wish to replicate onto our guest OS. Oh ho the fun! Here goes…
We’ll start from scratch on your developer (host) machine with no prerequisites. Here are the steps
* Install the VirtualBox program
sudo aptitude install virtualbox
* Install 7zip, needed to extract downloaded image
sudo aptitude install p7zip
* Download Ubuntu 8.04 Hardy Heron iso image from http://virtualbox.wordpress.com/images/ubuntu/
* Make a new folder called my_temp
Move the downloaded file into my_temp
cd into my_temp
* Extract image
p7zip -d downloaded_imagefile
This should yield a folder called something like ubuntu-8.04-x86.
cd into this directory and run
- ‘mv VDI/ubuntu-8.04-x86 ~/.VirtualBox/VDI’
* Create a new virtual machine
- Run VirtualBox (should be in Accessories under the Applications menu or just type ‘virtualbox’ at the command line)
- Click the New button to create a new virtual machine
- Create a name for your Virtual Machine and set it’s OS Type
- Set the base memory to 256MB
- Elect to add an ‘Existing Hard Disk’, you the Add button and choose the ubuntu-8.04-x86.vdi under the ~/.VirtualBox/VDI directory
* Run and customise new virtual machine
- You should now have a new Virtual Ubuntu machine at your disposal. Start it up by clicking the Start button.
- Log in with ubuntu/reverse as username password. Double check that this is correct by looking at the site you downloaded the image from
- Once logged in change the password through the System/Administration/Users and Groups menu
- Install ‘Guest Host Additions’ from the Devices menu of the running window of you virtual machine. GHA allow you to copy and paste between the host and guest OS’s, along with a raft of other improvements. Tip: If your mouse gets stuck in the virtual machine window then use the right control key to let it escape.
If the GHA install does not work (due to a timeout) then you will need to install the Guest Host Additions manually, as follows
i) On your real Ubuntu machine (ie. the host) download the file that was specified as a URL in the popup that was displayed when you previously tried to install Guest Host Additions - if you forget the URL try to download the GHA again from the Devices menu and when the URL appears copy and paste it into FireFox running on your host machine. Note: The GHA are installed onto your host machine.
ii) Once downloaded, move the file to /usr/share/virtualbox on your host machine and create the following symlink (as virtualbox won’t recognise the file if it has the revision number in the filename)
sudo ln -s /usr/share/virtualbox/VBoxGuestAdditions_2.0.4.iso /usr/share/virtualbox/VBoxGuestAdditions.iso
iv) Once again try to install ‘Guest Host Additions’ from the Devices menu of the running window of you virtual machine. This time it will pick up the local file you downloaded.
* Create a shared folder - to swap files between your host and your VM
- On your host machine create a directory you will use for sharing
- On your running virtual machine create a directory you will use for sharing
- From the Devices menu of the running VirtualBox windows select Shared Folders and set the ‘folder path’ to the folder you just created on your host. Give it a friendly name in the ‘folder name’ field and elect to make it permanent. Then on your VM run
mount -t vboxsf the_friendly_name /path/to/folder/on/virtual/machine
Note: That vboxsf is the filesystem type that VirtualBox is using for shared folders
Now you should be ready to go with your VM. Congratulations!
As a final step you may want to take this vanilla Ubuntu machine update it so that it has the same packages as another machine, in the case where you are looking to replicate an environment. Use dselect and apt to do this as laid out in the following guide Restore Packages using dselect on Kevin von Zonneveld’s blog.
As a final step you may want to install any other software that is not covered by aptitude packages - such as programs for which no debian package exists or for things like Ruby on Rails gems. It shouldn’t take you long to put these on your new virtual machine and “hey presto!” you’re ready to rock!
Posted in Linux, Programming, RubyIreland, Ubuntu, UbuntuIreland | 2 Comments »
March 9th, 2009
One of the key points of the Blackout Ireland campaign is that letting individual companies control the Internet is not acceptable. This article at OpenWriters.org says why.
Posted in Digital Choice, General Computers, Opinion Piece, Politics | No Comments »
March 5th, 2009
The battle for control of the Internet has begun within Ireland. In the red corner, Eircom has sided with the Big Four record companies - Sony-BMG, Universal and Warner and EMI - who are seeking to control the Irish Internet using coporate censorship and civil law techniques. In the blue corner, the Blackout Ireland campaign is looking to highlight the dangers of this censorship and has used social networking tools such as Twitter and Facebook to build support to protest this decision. A similar campaign was succesful in New Zealand, will it work again here?
Read more on OpenWriters.org.
Posted in Digital Choice, Linux, Opinion Piece, Politics, RubyIreland, UbuntuIreland | 1 Comment »
March 4th, 2009
This was not easy for me to find! My beloved Ruby on Rails apps just sitting there untweaked! Google and even the Passenger website didn’t specify where the config file may be found. And eventually it made a startling difference to my apps performance - possibly given that I’m trying to host a few sites at once - so it would be nice if it was easier to learn about how to setup the config file when new to Passenger,
Out of the box on a vanilla passenger install there is no passenger configuration file. However, there are a lots of configuration options and you can either dump these into
a) /etc/apache2/apache2.conf file (messy) or
b) Create a new file called passenger.conf under the /etc/apache2/conf.d directory as files in thei directory automatically get loaded by apache
c) Create a passenger.load file in mods-available and then enable the module
By the way, this setup is on Ubuntu, your mileage may vary depending on your distro. For an example, passenger.conf file see this one, just be sure to get the ‘LoadModule passenger_module’, ‘PassengerRoot’ and ‘PassengerRuby’ settings correct. You may already have these specified correctly in your apache2.conf file if you already have passenger working; if so you can reuse these values.
Apparently the recommended PassengerMaxPoolSize is 2 if you’re on a 256 MB Virtual Private Server System that’s running things like MySQL and keep the PassengerMaxInstancesPerApp smaller than this. If you’re running a couple of rails sites then maybe set PassengerMaxInstancesPerApp to 1 if you want to have 1 instance always available for each site. A RailsSpawnMethod of ’smart’ can also lead to better performance depending on your setup.
Certainly playing around with this config file helped me greatly improve my sites’ responsiveness for little effort and in particular it got the usage of the all important swap space down.
One final tip for today is to install htop (is available through aptitude). This gives a very useful and pretty display of the ‘top’ commands information and can make monitoring things like memory and swap at a glance much easier. So armed with this knowledge go forth and spawn! Well, till you run out of memory at least!
Posted in Linux, Programming, Rubyonrails, Ubuntu | No Comments »
February 20th, 2009
1) When you browse all your hosted sites and they’re all down then there’s a good chance Apache is too. Do a ’sudo /etc/init.d/apache2 restart’ and pay special attention to see if the ‘fail’ message appears. Not sure if you get as good an indication that the server is ‘ok’ if you just do the more graceful ’sudo /etc/init.d/apache2 restart’
2) It goes without saying but check your log files, especially the error.log file at /var/log/apache2
3) Under /etc/apache2/sites-available/my_cool_website.com, ensure that any custom ErrorLog’s or CustomLog’s you have set actually exist on disk - or else apache may fail to restart
4) Restart, restart, restart! When you enable sites or modules try not to forget to restart apache. It easy to change a setting such as the server url of your site in a sites-available file and forget the all import restart. You’d think I did this before!
5) When you migrate servers don’t forget to set your application or site directories to the appropriate user. This is distro specific; on Red Hat and Cent OS this is ‘apache’, on Ubuntu this is ‘www-data’. Others could be different again.
Bonus tip: Apache comes with mod_rewrite installed but not enabled. If you need it, be sure to enable it using a ’sudo a2enmod rewrite’ and ya, you guessed it, restart apache!
Posted in Uncategorized | No Comments »
February 19th, 2009
Hi folks! The (hardworking) Irish Penguin has just been resolving some issues that led to a site outage last night due to a server migration that almost went smoothly! Apologies if it led to any inconvenience. All it takes is one little misplaced command!
The outage has been resolved and will keep you posted if I notice any other issues.
Happy hackin!
Posted in Uncategorized | No Comments »
January 22nd, 2009
This was an ‘orrible bug. Tired and midway through writing up a Ruby on Rails tutorial on setting up emailing with AR Mailer and a little postfix configuration I wasn’t exactly in the mood for a bizzarely cryptic issue to waste my time. Enter the mysterious create_time_zone_conversion_attribute? method in all it’s glory. Basically you will get this error if you set …
config.cache_classes = false
… wherever it is set in your environment files (in my case config/development.rb) but the error goes away if you change it to …
config.cache_classes = true
… bloody daft! Here is the bug on lighthouse. Arrgh!!!!
Anyway here is the full stack trace which may help someone googling to pick up this page
NoMethodError in UsersController#create
You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.include?
/usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/attribute_methods.rb:142:in `create_time_zone_conversion_attribute?’
/usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/attribute_methods.rb:75:in `define_attribute_methods’
/usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/attribute_methods.rb:71:in `each’
/usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/attribute_methods.rb:71:in `define_attribute_methods’
/usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/attribute_methods.rb:350:in `respond_to?’
/usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/base.rb:2587:in `attributes=’
/usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/base.rb:2583:in `each’
/usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/base.rb:2583:in `attributes=’
/usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/base.rb:2283:in `initialize’
/usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/base.rb:691:in `new’
/usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/base.rb:691:in `create’
vendor/gems/ar_mailer-1.3.1/lib/action_mailer/ar_mailer.rb:92:in `perform_delivery_activerecord’
vendor/gems/ar_mailer-1.3.1/lib/action_mailer/ar_mailer.rb:91:in `each’
vendor/gems/ar_mailer-1.3.1/lib/action_mailer/ar_mailer.rb:91:in `perform_delivery_activerecord’
/usr/lib/ruby/gems/1.8/gems/actionmailer-2.2.2/lib/action_mailer/base.rb:526:in `__send__’
/usr/lib/ruby/gems/1.8/gems/actionmailer-2.2.2/lib/action_mailer/base.rb:526:in `deliver!’
/usr/lib/ruby/gems/1.8/gems/actionmailer-2.2.2/lib/action_mailer/base.rb:392:in `method_missing’
app/controllers/users_controller.rb:47:in `create’
app/controllers/users_controller.rb:45:in `create’
Happy hackin’
Posted in Geek, Programming, RubyIreland, Rubyonrails | 5 Comments »
January 16th, 2009
Ah such is the life. Chillin’ in Havana (the bar, unfortunately not the city), hackin’ away on Gruff of all things. This is the my first attempt at development on a gem and it has to be said is surprisingly satisfying. Getting gems to build is a doddle. Here’s a great link that shows how it’s done in moments. Also a handy thing to know is how to suck the code out of your OS’s gems directory and unpack them to under your app’s vendor/gems directory - all courtesy of VendorEverything. You can create the vendor/gems directory manually if it doesn’t already exist.
When developing on the gem, I found that the best strategy was to clone down the gem source code from GitHub or Rubyforge to under the vendor/gems directory of my application rather than using the OS package unpacking technique of Vendor Everything (though they do it more for deployment than development - which is a great idea). That way I could easily stay on top of the latest source code using git. Actually, to make things even cleaner I downloaded the Gruff gem source code to a different directory totally unrelated to my sample project and then symlinked to it from under vendor/gems of my app. Clean as an (unused) whistle!
And with Gem, Git and GitHub behind me life was trouble free. Now back to my search for great Cuban cigars…
Posted in Geek, Programming, RubyIreland, Rubyonrails | No Comments »