The Irish Penguin Watching Open Source unfold across Ireland

16Jan/090

Hackin on Gruff – A Brief Overview of Gem Development with Ruby

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

12Jan/091

Did Vista contribute to Dell’s Limerick closure?

Chris Mellor's recent article on Channelregister.co.uk concerning Dell's Limerick manufacturing exodus covered various reasons for Dell's decision. Many of these break no new ground - it is simply the economics of the situation Dell finds itself in. But there were some subtleties that he unearthed that haven't been stated by many commentators. Firstly, had there been sufficient demand for the notebooks that Dell were producing, then it would have taken both the Irish and Polish plants to satisfy that demand. The downturn clearly put a dramatic dent in the purchasing of notebooks but even before that Dell had been overtaken by HP as the world's biggest supplier; and both Dell and HP were suffering at the hands of Apple. In June 2008, Jordan Golson reported in The Industry Standard that at Apple notebook sales were up 61% since Q1 '07. It seems that the Leopards and Tigers that populate Steve Jobs virtual zoo of operating systems were propelling Macbook sales to new heights while Vista was dragging Windows Notebook sales to down to Davy Jones locker.

Meanwhile a completely new category of computers arrived - Netbooks - small low cost devices, pioneered by Asus and their initial range of Linux-based machines. These began to cut into the low end of the laptop sales and proved to be a disruptive force in the market. After years where people were paying extra money for small form factor laptops, Asus came along and made the cost of small portables proportional to their size, in a game changing move that would haunt the mainstream laptop manufacturers as time wore on. Things got so bad for Vista's adoption figures that Microsoft had to begin counting Windows XP sales as Vista sales via their controversial 'downgrade licence' program that allowed the likes of Dell to continue offering XP as an operating system on the machines they sold and allowed Microsoft to claim that another copy of Vista was now in circulation. But it wasn't the kind of thing that marketing dreams are made of. Even in August of 2008 research suggested that more than one in three new PCs were still being downgraded to from Vista to XP.

So we set off into 2009 with more battles looming in the desktop, notebook, netbook and server markets. And who knows, maybe a completely new computing category will yet again be invented this year. Apple, Windows and Linux continue to vie for the position as the OS supreme. And with Internet Explorer's market share falling to 68% from near dominance a few years ago,  gaps are still appearing in the browser market that will encourage Mozilla, Google and Apple to continue to innovate with Firefox, Chrome and Safari.

Windows 7 is somewhere on the horizon. It will come too late for Dell and its Limerick workers - will it come too late for Microsoft?

10Jan/090

Uninstalling Gems from ~/.gem in your Home Directory

"It's installed Gem but not as we know it"
-- Bones, if he was a Ruby Developer --

Unfortunately, a careless keystroke can leave gems in lots of unusual places. About five minutes ago, that fate befell me as I ended up with gems under my home directory at ~/.gems. Not what I was expecting. To make matters worse when I tried to remove them I had no success - possibly there were PATH or GEM_PATH issues

Fortunately, Google was to the rescue before you could say 'gem is no apt-get', with the command...

gem uninstall ruby-debug-ide --install-dir $HOME/.gem/ruby/1.8

... where in this case ruby-debug-ide is the misplaced gem in question.

Thanks to this thread for the solution.

10Dec/080

Better to be Oriented than an Object

Bit of a mixed bag of thoughts going into this post. Originally it was motivated by a couple of interesting points that I came across a bunch of ideas on programming by Kristian Dupont but then it turned into one of those link hopping sessions where you end up click through a wealth of interesting material.

A Tip or Two for your Troubles

Kristian outlined a good test to determine if adding a comment to your code is justified saying that "they should contain the word 'because'. This way, you know that you are answering a why rather than a what." That's a great test I think. If your comment fails the test then you may well be more in need of a refactor than a comment.

Secondly, when thinking about the classes you plan to use in your code, especially domain models, bear in mind that you're not actually trying to model the real world in a physical sense. For example, if you're writing software for a car dealership and one of your classes is going to be a car remember "You're not implementing a car. You're probably implementing a record of a car which can carry a number of stock items from one city to another, with a certain mileage and speed." The key thing is the context of what you're trying to implement and focus on that - often the real world will map nicely to your domain model but don't force things where doesn't. To shoehorn irrelevant aspects of the physical world into your models doesn't make it "more object oriented" or more correct - quite the reverse. Generally, this in not a problem with the attributes for your models, rather it is a problem with relationships and in particular with inheritance. Should a subclass really be a subclass - just because it is in the real world doesn't necessarily mean that your software be designed on the foundation that it is. This brings on nicely onto the other article, The Rise & Fall of Object Orientation, which ended up being another inspiration for this post.

What is Object Oriented these days anyway?

Danny Kalev argues that traditional OO is something that is less and less likely to be found in areas such as STL library of C++, as techniques such as inheritance give way to more template driven styles of programming. The problem he states "is that a member function is useful only for a single class" and goes on to highlight how this doesn't work well for things such as a library of containers.

"If each container class defines its own size() member function, you will end up writing numerous size() functions that basically do the same thing."

Better to use a standalone function to solve the problem which can be used across the board by any class irrespective of inheritance hierarchies. It isn't just C++ that has looked to functional programming techniques to solve problems that traditional OO couldn't deliver on. Languages such as Ruby have effectively made blocks and closures one of the core and most used features of the language. All iteration is done through blocks and lamba functions can solve tricky problems in an elegant manner.

It seems that traditional OO struggles most with collections but works well enough for single objects. Neal Ford thinks that Object Oriented Development is the wrong way to even frame the question of code reuse. Instead the focus should be on Library Oriented Development. “Ultimately, the class is just too fine grained to really achieve reuse, but libraries can.” On Java he states that while it “is a library oriented language, no good facilities exist within the language to manage libraries" and notes it sorely lacks a fixed directory structure such as in Ruby on Rails, where plugin developers know the layout of the projects they will be interfacing with from the get go.

Whether you prefer to consider your approach to software development an Object or Library Oriented approach it does seem that the modern developer will be drawing on a wide range of programming paradigms that go beyond traditional OO philosophy. In a hotly debated and opinionated field, it appears that the one true killer approach has not yet emerged to truly simplify and streamline development. At least it will continue to provide software developers, designers and architects with plenty to debate during their coffee breaks for years to come!

23Nov/080

3 Cheers for OSS Barcamp Dublin!

Another exciting event has just been announced for Dublin next year with an Open Source Software Barcamp planned for Dublin March 28th next year. And you could be one of the speakers! The idea behind a barcamp is to have a casual relaxed atmosphere where the attendees can help drive the schedule - like a conference done in the spirit of Digg. For more info check out Laura Czajkowski's blog post and if you want to keep up to date with further details you can track things on ossbarcamp on twitter.

9Oct/080

Troubleshooting fcgi and the dreaded undefined method `require_gem’ error

It happens to the best of us. We've coded our app go to deploy it and encounter some dispatch.fcgi issue. Today was the turn of
<br />
dispatch.fcgi error: undefined method `require_gem' for main:Object (NoMethodError)<br />

Well way to troubleshoot and arrive at a solution is to actually run ./dispatch.fcgi. I didn't know you could do this! It's well handy! If you are on a remote server to which you don't have ssh access then you can probably still run it as a cron job. Something like cd /path/to/my/app/public && ./dispatch.fcgi should do it. Remember that the dispatch.fcgi file is in the public directory of your application. The combination of running ./dispatch.fcgi and viewing the Rails log file revealed the useful error message above. Then a quick google search revealed that we need to change require_gem to simply gem (see here).

I had to change this in the dispatch.fcgi and then grepped the whole application directory for any instances of require_gem and changed them to gem too.

2Aug/085

Getting Ruby Plasmoids up and running in KDE 4.1 (on Kubuntu)

NOTE: THIS ARTICLE IS NOT YET COMPLETE. IF YOU FOLLOW IT THEN YOU WILL GO A LONG WAY BUT THE PLASMOID IS NOT YET DISPLAYING. SEE THE 'Run your applet' SECTION NEAR THE END OF THIS ARTICLE BEFORE PROCEEDING.

Introduction
KDE 4.1 is an absolutely terrific release of the KDE desktop, licking most of the issues presented in the original 4.0 release. Although it is still an early adopter release, 4.1 will be of interest to techie's who want to get their mitts on a sexy new graphical interface and is reasonably stable for daily usage. Not quite ready for Aunt Tillie yet though! One of the nicest features is the beauty and simplicity of Plasmoids and these super flexible little gadgets can now be written in Ruby - well almost!

This guide assumes that you're using Kubuntu Hardy Heron. But you should be able to adjust the instructions for any distro.

  • The starting point for our tour is to have Ruby installed. Google this if you're unsure as to whether or not it's installed.
  • We need to install cmake via 'sudo aptitude install cmake'. This is a cross-platform build system, similar to make.
  • It's likely that you will need to install the build UNIX build tools via 'aptitude search build-essential'.

Get coding!
And now you're be ready to go. The main tutorial out there on Ruby Plasmoids at time of writing is the Ruby Applet tutorial on KDE Techbase. This is a very good, well written guide on creating a simple Ruby applet in which a web browser is embeded inside the plasmoid. But the guide has a couple of gotcha's, especially for the newbie (myself included!), so we have listed solutions to these problems as we go. In summary, use our article as an supplement to KDE Techbase's.

Ok, to start with a demo everyone wants a nicely packaged directory of files, so let's quickly put together the basic directory structure for our plasmoid - it's pretty simple!

  • Create a directory called 'ruby-web-applet'
  • Save the files plasma-ruby-applet-web.desktop and web_applet.rb into this directory.
  • Create a file called CMakeLists.txt under your ruby-web-applet directory and paste the following code into it
set(SERVICES_INSTALL_DIR /usr/lib/kde4/share/kde4/services/)
set(DATA_INSTALL_DIR /usr/lib/kde4/share/kde4/apps/)

FIND_PACKAGE (Ruby REQUIRED)

include_directories( ${RUBY_INCLUDE_PATH} )
install(FILES plasma-ruby-applet-web.desktop DESTINATION ${SERVICES_INSTALL_DIR})
install(FILES web_applet.rb DESTINATION ${DATA_INSTALL_DIR}/plasma_ruby_web_applet)

Go to KDE Techbase
Once you have the above directory in place, go and read the Ruby Applet tutorial on KDE Techbase article down to just before the 'Install and test the Applet' paragraph. This will teach you what's going on in the code. At that point come back to me as I'll go into a little more detail on the final installation procedure!

You're back!
Great! Ok, so we have the files on disk, you have a good idea of what the code is doing, now let's install it and kick your plasmoid into action!

Open the CMakeLists.txt file listed above in a text editor. Change the path listed for KDE4 services and data directories to the ones on your system. These are the lines to change.

set(SERVICES_INSTALL_DIR /usr/lib/kde4/share/kde4/services/)
set(DATA_INSTALL_DIR /usr/lib/kde4/share/kde4/apps/)

Do this by replacing '/usr/lib/kde4/share/kde4/services/' with the output of the command 'kde4-config --install services' and replace '/usr/lib/kde4/share/kde4/apps/' with the output of the command 'kde4-config --install data' command. If you are using Kubuntu then they should be the same as mine but otherwise you may need to change them.

Then make sure you are inside your ruby-web-applet directory and type

cmake .

to generate nice makefiles for you which the make command can then use. Don't forget the trailing dot! Now type 'make' and 'sudo make install'. This will install your plasmoid code into the KDE4 services directory (you can see where this is by running 'kde4-config --install services') and your plasmoid data into the KDE4 data directory (you can see where this is by running 'kde4-config --install data'). Effectively, the installation just involves the two files - plasma-ruby-applet-web.desktop web_applet.rb. If it's worked then you'll get output like

Install the project...
-- Install configuration: ""
-- Installing: /usr/lib/kde4/share/kde4/services/plasma-ruby-applet-web.desktop
-- Installing: /usr/lib/kde4/share/kde4/apps/plasma_ruby_web_applet/web_applet.rb

The applet is now installed!

Run your applet (THIS DOES NOT WORK YET)
There are two ways to do this. In a developer kind of way you can run 'kbuildsycoca4' from the command line followed by 'plasmoidviewer plasma-ruby-web-applet'. Unfortunately this currently brings up the error

findServiceByDesktopPath:  not found
findServiceByDesktopPath:  not found

IF YOU HAVE A SOLUTION TO THIS THEN PLEASE COMMENT BELOW!!!

As a user, the cmake/make/make install procedure should have done all the necessary to install your plasmoid and you can activate it by going to the right of the task bar and clicking the Add Widgets icon. You should see your plasmoid listed but unfortunately this is not yet working - presumably for the same reason as the above. Oh well, I'm sure a solution will soon be posted. Until then, happy hackin!

Final notes

Note that the KDE techbase article provides a version of this file but it didn't work for me because

  • I knew nothing about cmake or cmake files and I didn't even cop that right clicking on the CMakeLists.txt file link in their article and saving it to disk would also save a load of HTML into the file
  • The line 'FIND_PACKAGE (RUBY REQUIRED)' should really be 'FIND_PACKAGE (Ruby REQUIRED)' as it's case sensitive and will otherwise give you the error

    CMake Error at CMakeLists.txt:6 (FIND_PACKAGE):
    find_package could not find module FindRUBY.cmake or a configuration file for package RUBY.

    Adjust CMAKE_MODULE_PATH to find FindRUBY.cmake or set RUBY_DIR to the directory containing a CMake configuration file for RUBY. The file will have one of the following names:

    RUBYConfig.cmake
    ruby-config.cmake

  • The destination targets specified in the file didn't work for me. Perhaps some environment variables need to be set somewhere
  • I didn't even know how to use cmake! Or that you needed to follow it up with 'make'. Anyway we'll come cover this soon!

I didn't need to set any CMAKE_MODULE_PATH but if you are having troubles with this then it's at /usr/share/cmake-2.6/Modules/ on Kubuntu Hardy Heron. You may be able to set it in your script if your having difficulties as follows 'set(CMAKE_MODULE_PATH /usr/share/cmake-2.6/Modules/)'. Again I haven't tried this my self!

25Jun/085

A Little Help on Importing Gmail Contacts using Ruby on Rails

We came across a fabulous article on getting your RoR app to pull contacts from your Gmail account at Atlantic Domain Solutions: Import Gmail Contacts using Ruby on Rails. Kudos to Atlantic Domain Solutions! This is a really great article. The only slight issue is that it omitted a couple of details which may confuse the newbie - in particular, where does all the code in points 1, 2 and 3 go and how do the code snippets relate to each other.

So here's a set of clarifications to the original article (a 'meta-guide' if you will), as we didn't want to just rip off these nice folks source code. Thus read these clarifications in conjunction with the original Atlantic Domain Solutions guide.

We'll assume that you know how to create a new rails app from scratch and create a controller. While you're following this you can use localhost as your server (eg. the default localhost:3000).

Create yourself a controller called ImportedContacts

class ImportedContactsController < ApplicationController

    def authenticate
        # Put the code from point 1 in the Atlantic Domain Solutions article here.

        # Note:
        # Set the next_param to the follow on controller action. For example,
        # next_param = url_for(:action => 'authorise')
    end

    def authorise
        # Put the code from point 2 in the article here.

        # Note:
        # I changed some of the code as shown below...
        # if resp.code == "200"
        #     token = ''
        #     data.split.each do |str|
        #         if not (str =~ /Token=/).nil?
        #             token = str.gsub(/Token=/, '')
        #         end
        #     end
        #     redirect_to(:action => 'import', :token => token)
        # else
        #     redirect_to ('/')
        # end
    end

    def import
        # Put the code point 3 from the article here.

        # Some notes:
        # Put the line 'authsub_token = params[:token]' at around line 4 of this method
        # (just after the two requires). It was accidentally omitted in the original article.
        # I also changed the local variable 'contacts' to '@contacts' so that it is available to views
    end
end

To see the contacts after importing, I created a folder called 'imported_contacts' under app/views and placed the file import.rhtml in it, containing the following code.

<table>
  <%= @contacts.map { |c| "<tr><td>Name: #{c['name']}</td><td>Email: #{c['email']}</td></tr>" } %>
</table>

Ok! You should be ready to rock! Ensure that you are not logged into gmail and visit http://localhost:3000/imported_contacts/authenticate

You should be brought to a Google account login page. Login and you will be brought to the Google page for allowing third party access to the Google API. Click 'Grant' and you should be brought to your app's import.rhtml page which should hopefully be displaying all your lovely contacts! Hurrah!

Blogged with the Flock Browser
19May/080

Quick Example of Serialisation via to_json in Ruby On Rails

Ruby On Rails provides a few neat ways to serialise/deserialise objects to JSON. Out of the box you get the following approaches

SERIALISING

* json_string = a_house.to_json

DESERIALISING

* an_object = ActiveSupport::JSON.decode(json_string) # gives you an object of type Object
* a_house.from_json(json_string) # gives you an object of type House, note that a_house
                                             # is an instance of House

These are pretty straightforward and do exactly what they say on the tin. Of the deserialising methods, note that using ActiveSupport directly gives you an object of type Object so you may prefer the stronger typing of the second technique which gives you an actual House object.

Note: There's been some great improvements to JSON support built right into Rails since Rails 2 has come around. For more info on this see here.

When things just aren't enough...
There are times when you will want to override the default serialisation that Rails provides - such as if you want to do some object 'flattening' like including an attribute from an association on serialising. Now before proceeding check out the latest Rails 2 JSON support mentioned at the end of the previous section as this might make your life a lot easier. Otherwise, let's press on with an example of DIY JSON handling.

Given a House object which has an associated Owner object, you might want to include the Owner's name attribute when serialising a House.

House Class
- has address and value attributes
- has an associated Owner object

Owner Class
- has a name attribute

We'll need to override the default to_json method on the House model.

class House::Base
  def to_json
    result = Hash.new

    self.class.content_columns.each do |column|
      if self.attributes.include?(column.name)
        result[column.name.to_sym] = self.send(column.name)
      end
    end

    if self.attributes.include?(column.name)
      result[:owner_name] = owner.name
    end

    result.to_json
  end
end

The above was partially taken from the howtogeneratejson page on the Rails wiki, which is has little bit more detail. Now all you need to do is call the a_house.to_json method and you get lots of lovely JSON which represents your model! One final note is that you may not want to override the default to_json method. If not, just call your method something else like to_custom_json. Also, there's

7Apr/080

Mercurial, Python and a Packet of Crisps

Came across this exciting bit of news on the ol wireless just recently that rails is moving from SVN to Git. Exciting times indeed as more and more developers are realising the merits of distributed source control to manage an ever expanding code base. This Irish Penguin's been in the fortunate position of working a lot with Mercurial, an alternative to Git, for the last few weeks and these types of tools are just a great way to work.

One of the big pluses is that your code and your repository (the history of all edits you've ever made to the code) is all in one directory which makes the process of source control really filesystem based. This is great when you're learning and just experimenting as you become more familiar with your new favourite toy. If you want to try something out, you just copy the directory somewhere and if it all goes to pot then it's just a quick copy command to restore it back to its original state!

If you're a Ruby or Python dev one of the really nice things about Mercurial (also known as hg - after the chemical symbol) is that it's done in Python, which means that it's pretty easy to hack a bit of code together if you want to extend functionality. A whole Saturday was lost to the cause of hacking Mercurial in Dublin's Central Hotel last weekend - as your host recovered from a bout of Extreme Coughing (there are more germs going round here at the moment than sheep in New Zealand!) over a pint of orange and a packet of crisps. Good fun! It was kind of weird though. As soon as urs truly had booted up the laptop, a bunch of folks grabbed seats to the back of the room muttering about Ubuntu cds and wifi. Sounds like Linux is all over Dublin like a rash. A very nice rash of course!