Archive for the ‘Geek’ Category

Santa’s got Gems baby! Ruby Ireland Christmas Meetup 2009

Friday, December 18th, 2009

Ho ho ho! The month’s Ruby Ireland meetup sprag right out of the traps with early adopters showing up at 6pm in the lobby area of the Trinity Capital Hotel, Wed Dec 16th. Easing into the evening with a 4 euro pint and talk of Android phones – seemingly the top item of everyone’s Christmas shopping list – the latest crop of gems in the Ruby world was in hot debate, gemcutter in particular.

A couple of folks had been playing around with RubyGame for visualising data as it changes on the fly – showing that this framework is for more than just gaming. The XML/HTML parser Nokogiri was also mentioned a few of times in passing, with the particularly eye-catching quote “XML is like violence – if it doesn’t solve your problems, you are not using enough of it” adorning the home page of its website. And the cracking little tool tig was also brought up, which has a dinky little ncurses interface into git repositories. Pretty cool; not least because it makes it easier for newbies to avoid being bitten when they start git’tin.

The downstairs lobby in the hotel worked out great for people to meet up and relax, with most people turning up at the scheduled 7 o clock for kick off. From there we took over the, what has to be said, pretty classy meeting room complete with old style couches and some Joan Miró paintings. Just in tune with the creative buzz we had going on. There wasn’t too much talk of Ruby for a while as most people were in stunned admiration of the room. Then the food platter arrived. Impressively, this is when everyone showed off their good manners by looking shyly at the platter for a few minutes, with that kind of “You first, sir” glint in their eye, before taking the plunge and sinking into the pakoras and wedges! Pretty much undoing any good work in the gym from earlier in the day!

One of the funnier moments of the night was when someone went to check the tweets against the (now settled upon) #rubyireland hashtag. Only to find lost rubyists tweeting from the hotel lobby as to where the meetup was on. After a quick runaround the lobby to herd anyone wielding a Macbook into the meeting room, the evening was back on track. We split up into a few smaller groups, with the main walk-through being on the qtonrails – a Rails plugin to simply developing applications on Linux and other platforms using Nokia’s Qt framework atop Rails.

To finish off we had a bit of improv comedy from everyone at different closing stages of the evening; in particular Paul O’Malley with his faithful rendition of an emotion beekeeper. And yes now we’re straying off topic so it’s probably time to go. We’ll leave you with Paul’s write up of last night’s shenanigans :-)

Thanks to everyone who showed. Have a great Christmas and catch ye all in Jan 2010 – surely destined to be the decade of Ruby domination!

Ciao,
Dec

Bleak House – A Tool for measuring Objects in Memory for a Ruby Program

Wednesday, October 28th, 2009

Bleak House is a tool that tells us
- How many Slots there are in total at a point in time in a Ruby program
- How many Slots in total are filled
- How many Slots in total are empty (free)
- How many Filled Slots can be attributed to a particular line of code

Bleakhouse can be used to tell you if program is holding on to objects that it should be relinquishing. But it doesn’t tell you how much data is stored in memory for the ‘meat’ of the object (ie. that 50MB of data in a 50MB String). Just because you know there is a Filled Slot exists – you don’t know if the data in memory that correlates back to that Slot is 1MB, 10MB or 100MB.

However, if you repeat a series of a specific set of operations a small number of times, measuring with Bleakhouse, and then restart the server with Bleak house and repeat the operations a large number of times and see a big difference in the number of filled slots can tell that your program is holding onto objects (references) that it should not. Of course, if your program is supposed to keep hold of an increasing number of references (such as a global variable or a singleton that keeps accumulating references for the duration of your program) then this would be expected. Though you might want to double check your design. You will be able to see the cause of the problem from the detailed breakdown of which lines of code were the biggest offenders in terms of creating objects. If you see a large number of free slots (relative to the number of filled slots) then this means that at some point in your program a lot of objects existed (possibly due to a spike in application usage) but then reduced.

Does the free slots count matter? Well, yes because there is an memory overhead due to each free slot that exists – how much depends on your particular system. If your system has a slot size of 20 bytes then every one million free slots costs you an additional 20MB that is not being utilised. This becomes a problem if your application is subject to large but infrequent spikes in the number of objects that exist within your program a particular moment in time because the free slots are taking up significant amounts of memory even when your application is twiddling its thumbs between the spikes.

Simple straight up caching for pages served by Heroku

Wednesday, September 16th, 2009

So you’ve got an app that’s ticking along nicely; being served up a good steak in a 5 star restaurant – but you’d like to boost it’s performance with some caching. For those who develop their apps on the Heroku platform, a great way to do this is to cache a dynamic page using Varnish. This means that your page is served up super fast without hitting Rails/Sinatra/whatever. And best of all it requires no extra gems or anything, just a well placed one-liner in your controller.

Firstly, you can only use this technique if all users that visit this page expect to see the exact same content – in other words you have no ‘per user’ customised content on a page. To help understand how this type of caching works, imagine that the first time your page (let’s say an Events index page) is hit it is turned into a static html page for a pre-defined amount of time (let’s say 60 seconds). Anyone else who visit this page (ie. anyone else who visits this particular controller action) during the next 60 seconds gets that static html page. After the 60 seconds the static html page is removed from the cache. Thus the next hit will cause your underlying dynamic page to be invoked; then the caching process kicks off again lasting another 60 seconds. And so on and so fourth.

With the increasing amount of web applications that call APIs, such as Twitter’s API, this is a really easy way to ensure that you do not end up spamming a service provider with an unreasonable number of calls per hour. This is the technique we use on www.thelisbontweety.com to keep our API overhead down.

So how do you do this? Simply put something along the lines of

response.headers['Cache-Control'] = ‘public, max-age=60′

as the first line of your action for the page you wish to cache. The max-age setting means that this will be cached for 60 seconds. After you put this in your application and redeploy to Heroku, you can see if it’s working by using http://hurl.it

Just enter the  URL for your action and click Send. You should see something like “Cache-Control: max-age=60, public” in the output if it’s working.

And that’s it! No need to install anything. Just cache your little heart out with Varnish. Top marks to chaps at Heroku for making this so easy to use out of the box at Heroku. For more on this technique check out their HTTP caching docs at http://docs.heroku.com/http-caching

Packaging Ruby Apps for Ubuntu: Dissecting an existing Ruby Ubuntu Package

Wednesday, September 9th, 2009

One of the best ways to learn about how a Ubuntu package is put together is reverse engineer the package into it’s constituent components. We are going to take a look at how to do this for the chef application and it’s related libchef library is packaged as a Debian package.

* Visit the page http://packages.ubuntu.com/karmic/ruby/chef
* Under the Download chef section, download the package via the ‘All’ link into a directory called chef
* Visit the page http://packages.ubuntu.com/karmic/ruby/libchef-ruby1.8
* Under the Download libchef-ruby1.8 section, download the package via the ‘All’ link into a directory called libchef1.8

From the following guide (http://www.g-loaded.eu/2008/01/28/how-to-extract-rpm-or-deb-packages) you can learn how to ‘unzip’ a Debian package. This is easy as they are pure ar archives. Here’s what we need to do

* In the chef directory, run the commands

ar vx chef_0.7.8-0ubuntu2_all.deb
tar -zxvf data.tar.gz

* In the libchef1.8 directory, run the commands

ar vx libchef-ruby_0.7.8-0ubuntu2_all.deb
tar -zxvf data.tar.gz

Now you can study the layout of the of the data payload of the package (this is where to look in order to study the anatomy of the application as it was being packaged). This layout is what will be of most interest to you.

If you have an application in a particular programming language that you wish to package, pick a similar application for which a package already exists and dissect it as shown above. Then bend your app into a similar shape in terms of directory layout before attempting to package it. To find out more about how to create your own Ubuntu packages check out this great video by Horst Jens Ubuntu: Making a .deb package out of a python program. It’s worth the effort of watching it to the end!

Happy packaging!

Bringing Back the Spirit of the Amateur Programmer

Wednesday, August 26th, 2009

In a blog post this month, Richard Dale (the man behind Qt/KDE’s Smoke bindings) eloquently phrased a noble goal,

“In the 1980s there were lots of computer magazines that used to publish programming articles with BASIC code, that everyone could input and run on their own computers. However, in the 1990s such large scale end user computer programming pretty much died out – tweaking the odd web page isn’t quite the same thing. One of the assumptions that the Free Software movement makes is that every user is also a programmer of some sort, who is able to tweak the software on their computers. I hope we can get back to that spirit, and change the way that people think about KDE programming, because at the moment there is a tendency to think it is hard and that only the ‘C++ gods’ like David Faure or Thiago Macieira can do it. In fact it is pretty easy to write small Python and Ruby apps and plasmoids, or to write a little script to message an app over DBus. We just need to get communities of like minded people together who write tutorials on TechBase, create blog entries with code (like the 1980s BASIC articles), and help beginners get started. These ubiquitous end user programming environments in Kubuntu (and other distributions I hope) will make it possible to do that.”

This really sums up something that would be fantastic to see over the next few years. There’s so many gadget lovers and technology geeks out there – the type of people who would’ve probably punched those BASIC tutorials into a Commodore 64, an Amstrad CPC464 or ZX Spectrum back in the good old days – that feel left behind as they perceive professional programmers to have blazed ahead a path that cannot be caught. But in many ways nothing could be further from the truth. For any programmer, there’s always some guy or gal that’s coding something more challenging or doing cleverer(er) stuff on the next machine. It’s all relative. And since software turned into a mainstream industry over the last couple of decades, it’s been the programmers doing the simplest tasks that have made the megabucks whilst the hardcore wizards of machine code and assembly have seen their demand dimish.

So next time you think there’s no point in picking up a few programming skills give a language like Ruby or Python a shot. Hopefully, with the continuing progress of Kubuntu and other distro’s to make programming more accessible, you’ll have the perfect environment to do so!

A Breath of Fresh Air – The Well Grounded Rubyist

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

Optimising Apache serving Ruby on Rails via Passenger

Monday, 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!

Bug of the Day: nil.include error with create_time_zone_conversion_attribute

Thursday, 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’

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

Friday, 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…

Just Diff the Internet

Sunday, June 22nd, 2008

Woke up this morning and found myself dead… Well, more to the point, my Internet connection dead. So being a Nymphobyte – someone feverously attracted to a reliable high bandwidth connection – there was little else to do than ponder the state of broadband in Ireland. Options are limited in the land currently home to the so called ‘Pariahs’ of Europe. Just make our broadband better and we’ll vote Yes! to Lisbon II and even Lisbon III ‘The Search for Spock’. Until then, you’re better off sending your packets of data back and forth via carrier pigeon rather than trying to connect via the empty promises of the current providers.

At least things aren’t as bad as in Sweden, oft thought of as land of the liberal and freedom loving Swedes, where their elected leaders are trying to sell them down the river – forcing a copy of all networked communications to be forwarded on the government so that they can, presumably, check for things like terrorists trying to share the latest episode of Desperate Housewives over Bittorrent. Sweden is now being mentioned in the same breath as China and Saudi Arabia with respect to Internet Freedom. According to an Information Age article even Google, not known to be adverse to collecting data on people, has been heavily critical of the crippling legislation. Blogger Oscar Swartz has a rallying mayday call on his site. Yes, that’s right, even the Scandinavians have given up on civil rights – now we know the world is doomed! They’re kind of like a civil liberties version of those budgies that used to warn of noxious fumes down the mine shafts in the old days – once they go tits up then you know the game is over.

Nope, freedom is not on the menu anywhere this week. An article in The Economist took stock of the scale of civil liberties that have been eroded under the Labour government and the relative apathy that this has been met with by the people. Gordon Brown, despite the having the popularity of Brian Cowan at a Eurocrat orgy, has even managed to push through legislation in Parliament to the effect that one can now be detained for 45 days at a go without charge. That’s over 6 weeks! You could miss the whole European championships if you’re unlucky enough! Which might suit you if you’re a Portuguese or Croatian fan, after their sides painful exists. But 6 weeks! Perhaps Gordon just has a secret repressed side that he didn’t know about – which is subconsciously upset that Ireland is now seen as more Eurosceptic than Britain. Forty five days isn’t so bad though. After all, Tony Blair wanted to lock ‘em up for 90 days without charge. Why not go the whole hog and just lock people up at birth? It saves all that awkward decision making and you wouldn’t even need National Identity Cards anymore. Mind you, who needs ID cards when everyone one has already gotten their own copy of secret government dossiers that they picked up on the Tube on the way to work. Now there’s a government I wouldn’t trust mind my dog for the weekend, never mind my confidential information. And I don’t even own a dog…

It’s been a tough week all right. It’s hard to know how to stay on top of things. My great plan is to do away with RSS feeds and subscriptions and just diff the Internet each morning before going to work. If there was some interesting news found it could be managed using git and deltas emailed to friends to keep them informed. Sure, it would be a lot of data to contend with, but there would be some scant satisfaction knowing that it would make those draconian Swedish government servers that spy on their citizens work that little bit harder!