Thursday, October 15, 2009

Rails Error: "interning empty string"

The "interning empty string" error kept popping up in some of our code written prior to upgrading to Ruby on Rails 2.3.4.
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/attribute_methods.rb:344:in `respond_to?'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/attribute_methods.rb:344:in `respond_to?'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/validations.rb:40:in `value'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/validations.rb:79:in `generate_message'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/validations.rb:30:in `message'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/validations.rb:34:in `full_message'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/validations.rb:275:in `full_messages'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/validations.rb:275:in `map'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/validations.rb:275:in `full_messages'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/base.rb:2036:in `inject'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/validations.rb:274:in `each'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/validations.rb:274:in `inject'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/validations.rb:274:in `full_messages'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.4/lib/action_view/helpers/active_record_helper.rb:201:in `error_messages_for'
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.4/lib/active_support/core_ext/enumerable.rb:59:in `map'
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.4/lib/active_support/core_ext/enumerable.rb:59:in `sum'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.4/lib/action_view/helpers/active_record_helper.rb:201:in `error_messages_for'
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.4/lib/active_support/core_ext/object/misc.rb:78:in `with_options'
C:/Ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.4/lib/action_view/helpers/active_record_helper.rb:192:in `error_messages_for'
C:/workspace/epanel_latest1/app/helpers/application_helper.rb:184:in `error_messages_for'
C:/workspace/epanel_latest1/app/views/login_users/login.html.erb:60:in `_run_erb_app47views47login_users47login46html46erb'

The culprit turned out to be:
model.errors.add('', 'Some message')
and the fix was to change this to
model.errors.add_to_base('Some message')

Wednesday, October 7, 2009

Rails: undefined method `use_transactional_fixtures=' in Rails 2.3

Problem: You try to run your Rails tests under Rails 2.3 and you run across the "undefined method `use_transactional_fixtures='" error.
./test/integration/../test_helper.rb:29: undefined method `use_transactional_fixtures=' for Test::Unit::TestCase:Class (NoMethodError)
        from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
        from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
        from ./test/integration/survey_user_interface_test.rb:1
        from /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5:in `load'
        from /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5
        from /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5:in `each'
        from /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5
rake aborted!
Command failed with status (1): [/usr/bin/ruby.exe -I"lib:test" "/usr/lib/r...]

Fix:
Change the class definition in test_helper.rb from
class Test::Unit::TestCase
to
class ActiveSupport::TestCase

Wednesday, September 2, 2009

MySQL Error: Can't connect to local MySQL server through socket

You try to run your Rails project and keep getting the "Can't connect to local MySQL server through socket" MySQL Error.
Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
/usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/connection_adapters/mysql_adapter.rb:548:in `real_connect'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/connection_adapters/mysql_adapter.rb:548:in `connect'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/connection_adapters/mysql_adapter.rb:198:in `initialize'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/connection_adapters/mysql_adapter.rb:74:in `new'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/connection_adapters/mysql_adapter.rb:74:in `mysql_connection'

The quickest way to fix this issue is to edit the database.yml file and replace localhost with 127.0.0.1.

Saturday, July 25, 2009

Why Programmers Should Use Portrait Mode Screens

Assume you have 1680x1050 (WSXGA+) monitor.

Window border + Menu bar + Tool Bars = 175px (vertical)
Status Bar + Horizontal Scroll Bar   =  55px
Total                                = 230px

Landscape Mode Active code area      = 1050px - 230px
                                     =  820px

Portrait  Mode Active code area      = 1680px - 230px
                                     = 1450px

Ratio                                = 1450/820
                                     = 1.768

Vertical coding area gained          = (1450-820)*100%/820
                                     = 76.83%

Friday, July 24, 2009

Crop Images in Batches Using ImageMagick and Ruby

Let us suppose you have an image named caps-1.png and you want to crop it to a 450x760 area, where top left corner of the crop region is the point (613,130) in the original image. You can easily do this by using the convert command that comes with ImageMagick. The command you might want to use might look like this:

convert "caps-1.png" -crop "450x760+613+130" "out/caps-1.png"

The numbers in the above are:
(613,130)  - top left corner
450x760    - the crop area (x,y)

Now let us suppose you have a directory full of images that need to cropped to the same dimensions as above at the same point as above. You can use a little Ruby snippet to execute the same convert command as above, but for each file in the current directory.

Dir.foreach('.'){|f| system  'convert \"%s\" -crop \"450x760+613+130\" \"out/%s\"' %[f, f.downcase] if f.downcase =~ /\.png$/}

This can be executed from the command line through the ruby interpreter:
ruby -e "Dir.foreach('.'){|f| system  'convert \"%s\" -crop \"450x760+613+130\" \"out/%s\"' %[f, f.downcase] if f.downcase =~ /\.png$/}"

If you want to simply generate script file with a series of convert commands, then simply replace the Ruby "system" statement with a "puts" statement:
ruby -e "Dir.foreach('.'){|f| puts    'convert \"%s\" -crop \"450x760+613+130\" \"out/%s\"' %[f, f.downcase] if f.downcase =~ /\.png$/}" > convert_files.sh

There the puts statements write the convert commands with parameters to standard out (STDOUT), and that output is redirected to the file convert_files.sh. Now you can set the executable bit of the script file
chmod +x convert_files.sh
and execute it over and over again.

If your source files are, say, PNG files but you want the convert command to save the output as JPEG files, use the following:
ruby -e "Dir.foreach('.'){|f| system  'convert \"%s\" -crop \"450x760+613+130\" \"out/%s\"' %[f, f.downcase.sub('.png', '.jpg')] if f.downcase =~ /\.png$/}"

For more information take a look at the "Command Line Processing" section of the ImageMagick manual:
http://www.imagemagick.org/script/command-line-processing.php#geometry


The commands listed here were used to extract the areas of interest of frames grabbed from an iPhone application demo video.

Friday, June 26, 2009

Firefox Shortcut to Quickly Access Labelled E-mails

You can use the "Quick Find Link", which is activated using the apostrophe (') key, to select a Label from the sidebar by typing in part or all of the Label name. You can use the F3 key to find the next matching link. Once th Label you wish to select is highlighted, pressing the Enter key activates the link, which will take you to page listing the e-mails which have been tagged with the selected Label.

Gmail natively allows you to do something similar by typing "in:label_name" in the search field (which can be activated by the "/" keyboard shortcut). However there are a couple of differences between this method and the previously mentioned Firefox specific method:

1. You need to type in the *full* Label name after the "in:" search parameter.
2. Gmail only displays 20 e-mails for each "results" page it returns, even if you have set the default value to 100 e-mails per page.

Saturday, March 21, 2009

Chrome Experiments

If you ever wondered how fast Google Chrome's V8 JavaScript engine is, you have to look no further. The recently launched ChomeExperiments.com has a number of computationally intensive demos written in JavaScript which will let you compare Chrome's JavaScript performance with that of your favourite browsers.

I tried out Monster on Firefox 2, Safari 3.1.1, and Google Chrome 0.4.154.


A Bit About Monster



Monster is 3D demo which uses the HTML Canvas element to render 3D animations. It starts with a square which morphs into a rotating cube which morphs into a sphere which morphs into a rotating "Monster".



Firefox 2.0.0


I wanted to see how well Monster performed in Firefox 2. Firefox showed a lot of promise as the initial square morphed to a cube and then a sphere. However, my optimism was short lived as the sphere morphed to a "Monster" and the rendering went from frames-per-second to seconds-per-frame. Saying "the demo did not perform well" would be a huge understatement.


Safari 3.1.1


The demo started a little slowly under Safari. After my experience running Monster under Firefox 2 I was expecting even less from Safari. But Safari surprised me by maintaining a low, but consistent, frame rate throughout the demo. The experience did not seem to degrade as the polygon count of the demo increased. Having said that, the performance was far from fluid.


Google Chrome


I had been disappointed by both Firefox 2 and Safari. I had seen the video clip, but I could not help wondering if it was something with the machine I was using. I fired up Chrome 0.4.154 which I had downloaded shortly after Chrome was launched. I waited as the square turned into a cube and then into a sphere and then into the "Monster" without skipping a beat.


Internet Explorer 6


We all know that Internet Explorer 6 does not have support for the HTML Canvas element, but I launched Monster in it just for the fun of it. The result? A JavaScript error. Enough said about IE!!!


Final Thoughts...


I knew Chrome was ahead of the pack in terms of JavaScript performance, but I had not realize by how much until I did a side-by-side comparison with the aforementioned browsers. I also need to try this Demo using Firefox 3, Safari 4, Internet Explorer 7, Internet Explorer 8, and Opera, but that will have to wait for now.