Posts Tagged ‘Ruby’

Ruby’s and Rails’ case gotcha when comparing classes

October 15th, 2008

While pairing yesterday I ran into quite an interesting problem when using the Ruby case statement. Now this should not come to a surprise because the case statement uses the === operator rather than the == operator (that is common in other languages). We were refactoring some code like this.


variable = group_class == Animal ? 8 : 2

initially this turned into


if group_class == Animal
  variable = 8
elsif group_class == Water
  variable = 2
else
  variable = nil
end

This code was simply meant to check against class type since group_class is holding the type of this object. We decided to change this into a case statement as we could see more group_class types coming down the road in the next iteration.


variable = case group_class
               when Animal: 3
               when Water: 8
               ....

So this code was assumingly supposed to work, however does not. Apparently the valid way of writing this class is to do:


variable = case group_class.name
               when "Animal": 3
               when "Water": 8
               ....

Then this code works as designed. Tracking down the reason for this is ActiveRecord’s overloaded == and === operators.


# File activerecord/lib/active_record/base.rb, line 1269
1269:       def ===(object)
1270:         object.is_a?(self)
1271:       end
-----
# File activerecord/lib/active_record/base.rb, line 2421
2421:       def ==(comparison_object)
2422:         comparison_object.equal?(self) ||
2423:           (comparison_object.instance_of?(self.class) &&
2424:             comparison_object.id == id &&
2425:             !comparison_object.new_record?)
2426:       end

Just a quirk I noticed. Enjoy :)

Tags: , , ,
Posted in Rails, Ruby | Comments (1)

Rubyme opens its doors.

August 20th, 2008

I have been working long hours getting this site up and running. The concept was initially drafted after some tutoring work I had done and the difficulty I had in coordinating everything. If you have not yet figured out, the site is called Rubyme (www.rubyme.net). The goal of the site is to provide an easy way for users to find help on anything from specific project difficulties to a more typical tutor style. Currently many of the features are still under heavy development, but the basic functionality is there. I encourage you to sign up and become a tutor. It is a great way to make some money, get exposure, and learn. There will be ongoing patches, so please report any problems to my email address: justin.smestad@gmail.com

Tags: , , , , ,
Posted in Development, JRuby, Merb, Rails, Ruby | Comments (0)

mod_rails setup issues and solution

August 20th, 2008

So I was migrating my server over to Debian Lenny from Etch and ran into some interesting problems along the way. The first of which is related to an open ticket on the mod_rails issue tracker. The conflict appears to be with Wordpress permalinks, .htaccess and mod_rails. However, without my knowing when I installed mod_rails (a.k.a Passenger) via the script it disabled by rewrite module. So when I went back to check this blog, running wordpress, all the permalinks were broken. I had tried everything from setting ‘RailsAutoDetect off’ and ‘RailsAllowModRewrite on’, however neither of these are even needed. I just had to issue the simple commands:
[code language="shell"]sudo a2enmod rewrite
sudo /etc/init.d/apache2 restart[/code]
After these two commands, you can stop pulling out your hair and begin shaking your fist at the sneaky passenger/mod_rails installer. Just goes to show that automated installers are not always the holy grail.

Tags: , , , , , ,
Posted in Rails, Ruby | Comments (1)

JtestR - alive and kicking

August 10th, 2008

Last week I posted about my JtestR problems and the lack of any solution. Well after some generous help from Ola Bini, JtestR’s lead developer, we found the problem. Apparently, you cannot run your Java application with the jtestr.jar file inside the root directory. The jar must be placed within a subdirectory, otherwise your Ruby runtime will try to include the JtestR.jar file through its require ‘jtestr’ statement. Its an odd quirk, with an easy workaround. Ola Bini has said its fixed in the latest trunk build, but to use the workaround until 0.4 comes out. I would like to thank him for all his help troubleshooting this issue. Cheers!

Tags: , , , , ,
Posted in JRuby, RSpec | Comments (0)

Ruby 1.8.6 / 1.8.7 living side-by-side

July 17th, 2008

With the most recent update of OSX, Apple did not update Ruby from 1.8.6 to 1.8.7 even though there are significant improvements incorporated into the new version. I decided that instead of waiting for Apple to update my default Ruby install, I would do it myself. My goal was to keep the default Apple Ruby install of 1.8.6 alone and install 1.8.7 through MacPorts. That was the easy part.
[code language="shell"]$ sudo port install ruby
$ sudo port install rb-rubygems[/code]
This will give you both ruby and a rubygems install. The way to make this installation the default is to adjust your PATH to have /opt/local/bin/ as the first item in your PATH definition (which is set in ~/.bash_profile).
[code language="shell"]PATH=/opt/local/bin:$PATH[/code]
Then reload your terminal or execute:
[code language="shell"]$ source ~/.bash_profile[/code]
Now here comes a nicety that many people will really appreciate. We want both version of Ruby to share the same gems! This is simply set (adjust paths accordingly if your not on OSX 10.5) the following inside your .bash_profile.
[code language="shell"]export GEM_HOME=`/usr/bin/gem env home`
export GEM_PATH=`/usr/bin/gem env path`[/code]
Make sure you use ` marks and not ‘ or “. This will use your 1.8.6 ruby gems as the source for your ruby gems. There you go, now you have two versions of Ruby living side by side in harmony, its so beautiful it brings a tear to my eye.

The goal of this is that you can leave the Apple install alone and if you ever want new (more current) ruby versions, you can just issue an update command via MacPorts. Thanks to drbrain for the help on troubleshooting this setup.

Tags: , , ,
Posted in Ruby | Comments (1)