Software dev, tech, mind hacks and the occasional personal bit

Category: Ruby / Rails Page 6 of 7

Is .NET or Java dying?

Are C# and .NET losing ground as Martin Fowler suggests? Or is Java’s market share dropping? What about Ruby? And what about the Australian market in particular?

Here’s what I’ve been able to find.

Job Trends
Which technologies have the most demand for people?

From Indeed.com, which claims to search “millions of jobs from thousands of job sites”, but I suspect may have a USA focus:

“Best Talent Index May 2007” from Best People Solutions gives an Australian perspective:

Here’s job counts from the (largest?) primarily Australian job search site Seek on 5 June 2007, 3pm (today):

Keyword(s) Number of positions found
Java 3,414
“.NET” or “dot net” 2,744
“c#” or “c sharp” 1,722
ruby 100

As an aside, I remember doing a search on Seek for “ruby” about 6 months ago, and getting under 20 jobs mentioning it.

Search Engine Number of Hits

Extract from the TIOBE Programming Community Index for June 2007:

Position
Jun 2007
Position
Jun 2006
Delta in Position Programming Language Ratings
Jun 2007
Delta
Jun 2006
Status
1 1 Java 20.025% -1.10% A
2 2 C 15.967% -2.29% A
3 3 C++ 11.118% +0.45% A
4 4 (Visual) Basic 9.332% -0.85% A
5 5 PHP 8.871% -0.72% A
6 6 Perl 6.177% +0.17% A
7 8 C# 3.483% +0.25% A
8 7 Python 3.161% -0.30% A
9 10 JavaScript 2.616% +1.16% A
10 19 Ruby 2.132% +1.65% A


I think this gives a good idea of web buzz, but suggest that most non-IT companies do not publish information about their projects and chosen technologies and languages on the web.

Conclusion
The data collected suggests that:

  • Both .NET and Java are major players in the job market with thousands of positions advertised, implying wide industry adoption of both.
  • Neither .NET nor Java seem to be undergoing any significant decline in jobs.
  • Java has much more information about it on the internet, although .NET is slowing gaining ground and Java slowly losing it.
  • Ruby is comparatively tiny but growing rapidly in terms of jobs and information on the internet.

Thanks
Thanks to Jason Yip and Suzi Edwards for their help finding/sourcing information.

Can you spot the bug?

In a model class which has ‘quantity’, ‘quantity_already_taken’ and ‘quantity_requested’ properties, I add the following:

def before_save
  quantity = 1 if quantity == 0
  if quantity + quantity_already_taken > quantity_requested
  ......

Tests blow up everywhere with:

TypeError: nil can't be coerced into Fixnum   (on the line with the addition)

Why?

After a little more debugging, it is clear that ‘quantity’ is nil. How could that happen?

The answer lies in the fact that Ruby requires an explicit self reference when using attribute writers (aka, property setters) within the class itself. This feels clunky to me, but for your information, here’s a rationalisation of the explicit self requirement.

So, in case you’re wondering, what happened above is that the ‘if’ line created a nil local variable called ‘quantity’! This local variable then had higher scope precedence than the class attribute with the same name. The addition line was then using the local ‘quantity’ rather than the class attribute and hence failed with the nil error.

All fixed by explicity referencing self:

def before_save
  self.quantity = 1 if (quantity == 0)
  if quantity + quantity_already_taken > quantity_requested
  ......

Tips for Developing Mephisto Plugins with Liquid and Rails

When I was writing a contact form plugin for Mephisto, I had a lot of trouble finding documentation and ended up reading lots of code and experimenting. That was fun, but fairly slow, so I hope this post can save future plugin developers time, and help them avoid some of the gotchas I stumbled over.

Repository Directory Structure
At the most macro level, your repository needs to have a ‘plugins’ directory, and then a directory named after your plugin. Eg,
…/plugins/my_new_plugin/…
If this is not set up correctly, your plugin will not be able to be installed via ‘ruby script/install plugin ‘ method.

Liquid Plugins Directory and Init.rb
As you probably know, Mephisto uses Liquid for page templates. Liquid can be extended with new tags/blocks. The way to do this in a plugin is to set up a ‘mephisto/liquid’ directory with your extensions in it. See example here. So that’s great, but you also need to register it in init.rb. Here’s the contact form’s init.rb – check out the line about ‘register_tag’.

Mephisto Plugin Class
Mephisto trunk now has a base class for plugins – Mephisto::Plugin. Inheriting from this allows you to set up routes to brand new controllers you create. See contact form example here. This opens the door to writing Mephisto plugins which do postback and processing. It is also possible to add in tabs and forms in the administration interface. Rick‘s feedback plugin shows how to do this.

Using Liquid Templates from Your Plugin
One of the trickiest bits was getting the plug-in controller to render a liquid template. This is important if you want your additions to Mephisto to have the same layout and colours as the rest of the site. The way I’ll outline below works fine, but it is not ideal. Hopefully there is a better way to do this (eg, some sort of Liquid API for Mephisto plugins).. if you know how a better way, please let me know!

I had my plugin controller inherit from the Mephisto ApplicationController to gain access to the method ‘render_liquid_template_for’. You can see the code here. However, this led to thorny problems where the plug-in classes were getting loaded only once when the server started, but Mephisto (and the ApplicationController) were getting reloaded for every request. First request worked fine, but nasty errors were spat out on the second and subsequent requests. To resolve this, I removed the plug-in from the ‘load_once_paths’. You can see how to do this in the init.rb.

Models, Views & Controllers Directories and Init.rb
Okay, this is open to personal taste. I like to have similar directories in my plugin to a normal app. Eg, separate directories for controllers, model, etc. This causes a bit more work, as you need to add the extra directories to various global path variables. For an example of how to do this, take a look at ‘models_path’ and ‘controllers_path’ in this init.rb and the physical directory structure of the contact form’s lib directory.

UPDATE
More info about Mephisto plugins with Drax 0.8.

Improve Rails Performance Through Eternal Browser Caching of Assets

I’ve been working on a rails app which has got quite a number of pages that share the same two css files and 3 javascript files. However, every time I visited any page of the app, all of javascripts and css files were being loaded from the server. Not good – site was very slow. Mucking around with ‘about:cache’ command in Firefox revealed that the css and javascript files had expiry dates set in the past – ie, no caching of them at all. Also, all the links to sylesheets and javascript files generated by rails had ?[some long number] after them. Some research on the web revealed that this is a new rails feature for caching – the long number is a timestamp for when the asset was last modified.

Okay, so why were these assets not being cached? A quick check with wget –save-headers revealed that the web server was sending a nocache directive to the browser. This seems to be the default setup for webrick and also for my shared apache hosting on railsplayground. Considering the new rails asset management system with the ?[last modified timestamp] in the URLs, nocache seems wrong. The browser should never expire the cache since rails will handle cache invalidation by updating the asset url with a new timestamp.

So, how can we implement no/very long cache expiry? In apache, you can use mod_expires or mod_headers to do this. My shared hosting does not support mod_expires, so I went for mod_headers in my .htaccess file.

Using mod_headers:

<FilesMatch "\.*$">
Header set Cache-Control "max-age=29030400"
</FilesMatch>

OR using mod_expires:

ExpiresActive On
ExpiresDefault "access plus 1 year"

Either of the the above will set up a cache expiry time of one year for all content (best you only do this for your rails app directories).

With a cache expiry time of one year in place, my rails apps run much much faster.

Starting at ThoughtWorks: First Five Weeks

Well, I’ve been with ThoughtWorks for just over 5 weeks now, and I thought I’d write down some thoughts before the hiring and joining process got lost in the misty swamp of my memory.

Hiring process
To cut a long story short, I did a phone screen with HR, a coding test and then some fairly quick aptitude and personality tests and finally 3 interviews. You have something like a week to do the coding test and then submit your code for review. I had the other tests and interviews on a single day. Although this sounds pretty horrendous, it actually wasn’t too bad. Tests were pretty quick and the interviewers were astoundingly friendly. I finished by something like 3pm in the afternoon, including a lunch break, and surprisingly didn’t feel too bad or stressed afterwards.

Induction
As a fairly impressive start, I had 2 days of induction in Melbourne (I live in Sydney). ThoughtWorks arranged drivers, hotel and flight so it was all very smooth. This was lucky as I was pretty jet lagged and confused – I’d just flown back from an overseas holiday not long before. Induction was largely getting a company provided laptop, meeting people and getting an introduction to various internal systems and procedures. As an aside, I’ve heard that there is now an “immersion” process where you get sent to India for a week or two for induction but can’t comment on that.

A few weeks on the beach
When you’re not assigned to a client project, you are “on the beach”. This means you go into your local office with your laptop. It’s really great – there’s no particular tasks assigned to you, but the opportunity is there to get involved in a lot of interesting stuff. To give you some examples, here’s some of the stuff I’ve had the chance to do:

  • Write an open source plug-in for Mephisto for ThoughtWorks Studios
  • Be involved in scoping out and estimating for a RFI from a new client
  • Pair with another developer to do code reviews of potential new recruits
  • Help out briefly with a fun project to develop a driver for a USB build light for continuous integration servers (red for broken build, green for good build, etc)
  • Help out on client projects – I was asked to whip up a little proof of concept for JRuby and Java integration and learnt a bit getting this set up
  • Do a little bit of Google Maps integration
  • Meet colleagues and learn more about procedures etc
  • Get invited to lunch with the managing director – this is something that happens for all new hires and I think it’s really great
  • Almost go out on a pre-sales call (I’ve got to go back to Melbourne and will miss this unfortunately)
  • Catch up on tech reading such as blogs, books etc
  • Go to a swanky talk given by Martin Fowler and Kristan Vingrys
  • Eat lots of free lunches (usually twice a week) and attend various talks at the office given by other consultants
  • Drink lots of free coffee (ThoughtWorks has a coffee tab with a local cafe)

First project
Much fun as it is on the beach, after a few weeks, I was itching to join the big boys and go on a project. Getting assigned to a project is the purview of your professional services manager, and can be pretty changeable. The saying is that “you don’t really know what project you’re on till you walk in the door of the client site” and I’ve even heard “you don’t really know what project you’re on till you’re on the plane home”. There’s a grain of truth in these – it can look like you are going to go on a project and then it doesn’t come through, or some other project becomes more important or whatever. I almost went on several different projects before finally ending up on quite a cool Ruby / Rails project with a startup in Melbourne. So, I got to join the jet set and have been flying down to Melbourne during the week, and back for the weekends. This is a bit tiring, but ThoughtWorks does its best to make things comfortable. I’m staying in a really nice corporate apartment in Melbourne, flights are arranged and paid for and drivers are scheduled for pickup and drop off to the airport. There’s also a generous per diem allowance for food. The project is really cool, and I’m enjoying it, but can’t say more as it is under a NDA.

Back on the beach.. but only for two days!
My first project was two weeks, so after that I returned to the beach. Today is my second day on the beach. However, it turns out that the client was very happy with our first two weeks work and they’ve invited us back again until Easter. This means I need to fly back to Melbourne tomorrow. This won’t continue indefinitely though – when I was discussing the project with my professional services manager, we agreed that I would not need to stay on a Melbourne project for more than 6 weeks. And clearly this is in ThoughtWorks interest as well – it costs a lot more to fly somebody in from Sydney every week and provide accommodation etc. I’m going to be transitioning off the project by Easter and a Melbourne based consultant is going to take over from me if the project continues further. It’ll be good to be on a Sydney based project again but I feel it would be unfair not to say that ThoughtWorks has done a really good job in making working in another city as convenient and pleasant as possible.

Various benefits
ThoughtWorks is pretty generous in the expenses department. They cover your mobile phone, home internet, per diem when away, give an allowance for training courses and books, etc. There’s also lots of free lunches, food, coffee and catered events.

Transparency and knowing what’s going on
I’ve been quite impressed to get a monthly update email that talks about ThoughtWorks plans, goals and financials, headcount etc in significant detail. There’s also various update meeting where you get to hear how projects are going and what’s happening with various clients. Personally I’m really glad to see this type of thing, as at previous jobs, this has been privileged information, and most of the time, I have not really had any idea how well the company is doing financially as a whole, or what the future plans and directions are.

Variety and Unpredictability
These are really two faces of the same coin, and depending on your character and experience, I think you might either love or hate this. You really don’t know what project you’re going to be working on, what your role will be, what industry the client is in, what type of development they need or for what platform or in what language. In fact, you don’t even know what city you’re going to be in during a given week. I’m enjoying this at this point as my last job was always in the same office, with the same technologies etc. However, I can see it may be trying in the long term, and it does make it difficult to do the shopping or organise things with friends during the week. On the other hand, I have heard that most of the work in Sydney is for big companies like banks and telcos in the city CBD within walking distance to the office, and the majority in Java. So perhaps my short experience so far is not the norm.

In conclusion…
So far, I can honestly say that it’s been really great working at ThoughtWorks. I’ve had a chance to do some of the stuff I’ve wanted to do for ages like work on a bit of open source and do some commercial Ruby on Rails work. My colleagues have been friendly and welcoming, and I’ve been wowed by the level of care that ThoughtWorks takes of its employees.

Contact / Feedback Form Plugin for Mephisto

Introduction
If you use Mephisto, a content management / blogging system written in Rails, you may well be interested in using this new plug-in. It provides a form that lets visitors to your site leave their contact details and send you messages or feedback via email.

UPDATE
Please check out information about using the contact form plugin with Mephisto Drax 0.8.

License
This plug-in was developed for the new ThoughtWorks Studios site. As I wrote it at and for work, it is copyright ThoughtWorks, 2007. However, ThoughtWorks, being generous souls, is happy for me to open source it under the Apache 2.0 licence, which pretty much means you have free reign to use it as you want.

Requirements

  • Mephisto Edge (the latest stable 0.7.3 release does not have support for Mephisto plugins)
  • Rails Edge (required by Mephisto edge)
  • ActionMailer (comes with Rails) correctly configured with SMTP server etc, so that emails can be delivered. See “Configuration” section here for more details.

Installation

ruby script/plugin install http://github.com/jcrisp/mephisto_contact_form/tree/master

or in your vendor/plugins directory for Mephisto:

git clone http://github.com/jcrisp/mephisto_contact_form/tree/master mephisto_contact_form

Make sure you restart your web server at this point so that the plugin is loaded.

Setup
1. Create a new template called ‘contact_us.liquid’ though the admin web interface (under the ‘Design’ tab).
Paste in the following code:

<H1>Contact Us</H1>
{% contactform %}
<p>{{ form.name }}<label for="author"><small>Your name</small></label></p>
<p>{{ form.email }}<label for="email"><small>Email address</small></label></p>
<p>{{ form.phone}}<label for="phone"><small>Phone number (optional)</small></label></p>
<p>{{ form.subject}}<label for="subject"><small>Subject</small></label></p>
<p>{{ form.body }}</p>
<p>{{ form.submit }}</p>
{% endcontactform %}

Feel free to modify labels, layout etc.

2. Edit

{MEPHISTO_ROOT}/vendor/plugins/mephisto_contact_form/lib/contact_notifier.rb

and put in the email address you want contact form submissions to go to.

3. Link to “/contact_form” from your site.

Any issues / questions / suggestions?
Best to post comments on this blog.

Technical Info
The contact form plugin is actually a combination of a rails plugin, a liquid block plugin and a Mephisto plugin. See this post about developing Mephisto plugins for more information.

_vimrc for Ruby

In the past I’ve used gvim for Ruby coding. It’s been pretty good, especially with the new tabbed editing and omni complete (bit like intellisense in Visual Studio) introduced in vim 7. However, when I downloaded vim at work, I was missing the secret sauce – a good vimrc. Here’s my usual vimrc for ruby:

set nocompatible
behave xtermset
selectmode=mouse
set nu
set tabstop=2
set shiftwidth=2
set softtabstop=2
set ai
set columns=100
set lines=70
set guifont=Courier:h10
set expandtab
set smarttab
let g:rubycomplete_rails = 1

Among other things, it makes the default window size bigger, uses a prettier font, sets up auto indenting ruby style, and turns on omni-complete.

By the way, on windows, assuming a default install, vimrc is to be found here:

C:\\Program Files\\Vim\\_vimrc

JRuby Setup

Recently got a JRuby/Rails system with Java integration up and running. Unfortunately, it took quite a few hours, as most of the docs and code you find through Google are out of date.

If you use JRuby 0.9.2 from Codehaus, you will get an error similar to this when you try to access a rails application:

[2007-02-26 17:54:59] INFO WEBrick::HTTPServer start: pid=22540508 port=3000
<ArgumentError: Anonymous modules have no name to be referenced by>
[“c:/jruby-0.9.2/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/
active_support/dependencies.rb:402:in `to_constant_name’…

If you’re stuck in this rut, fear not! Nick Sieger has written very helpful instructions which outline how to get and set up the latest development snapshot. Please note that in addition to the instructions, you need to set your JRUBY_HOME environment variable. Under Windows, do something like this:

set JRUBY_HOME=c:\jruby

If you’d prefer not to use the snapshot, you can get the source code through subversion from:

http://svn.codehaus.org/jruby/trunk/jruby

but at the time of this post, you need to run svn checkout or update with “–ignore-externals” to avoid the following error:

Error: URL ‘svn://rubyforge.org/var/svn/bfts/bfts/trunk’ doesn’t exist

Many thanks to Nick Sieger and the JRuby user mailing list for their help.

Using floating point variables to represent money => not a good idea!

I was reading through some code the other day and was surprised to find that it was using doubles to represent dollar amounts. Reason for the alarm bells is that doubles and floats cannot accurately represent many decimal fractions (eg, 0.1), since doubles and floats internally work with powers of 2 rather than powers of 10. These inaccuracies are likely to lead to significant errors, especially when performing arithmetic (eg, adding up a table of dollar amounts). See this IBM article for a more in depth explanation and examples. The solution is to use types that work with powers of ten internally. In C#, you can use ‘decimal’ and in Java or Ruby, ‘BigDecimal‘, to avoid these problems.

Thai Ordering and Development Mode vs Production Mode for Rails Apps

A while back, I wrote a little rails app for Thai food ordering at my work. My colleagues place orders using the system and then bring money to the nominated orderer of the week. Once all orders are in and paid for (this is also tracked in the app), the orderer rings up our favourite Thai restaurant (Laddas) and places the order.

I have the app running from fairly cheap shared hosting. At peak times during the ordering, I guess that they’d be 15 or so people simultaneously using the app. We’ve used it many times without problems. Thus, I was quite surprised and displeased (as were my colleagues), when my hosting account was suspended and we couldn’t see what had been ordered this morning. A hasty email to my hosting provider revealed that my account had been suspended due to high load and “ruby flooding”. They were kind enough to un-suspend my account and we completed the ordering process.

I remembered seeing something about production mode in ‘environment.rb‘. Some googling confirmed my hunch – in development mode, rails apps are much more resource intensive. Caching is not used, and every single file needs to be reloaded every time it is required. After changing my app to production mode, it seemed to run noticeably faster. Michael and I ran ‘top’ and it looked like each request used less CPU.

So, should you be in a similar situation, this is how to change your app to production mode on fast-cgi Apache shared hosting:

  1. Confirm that ‘database.yml‘ in your app’sconfig‘ directory has a section for production mode, and that it has up to date database connection details.
  2. Edit ‘environment.rb‘ in your app’sconfig‘ directory.
  3. Add this line:
         ENV['RAILS_ENV'] ||= 'production'
  4. Run ‘ps -u [your_user_name]’ to find if you have any ‘dispatch.fgi‘ processes running.
  5. If so, kill all of them (they’ll restart and use your new config settings).
  6. Browse to your app, it should now run faster.

Page 6 of 7

Powered by WordPress & Theme by Anders Norén