Chad Woolley's blog
Standup 01/17/2008
Interesting Things
There is a gotcha when creating a Ruby Hash with a default value. If you pass a object to the constructor, such as an empty hash, the same object will be used for all default values. That probably isn't what you want. Instead, use the form of the constructor which takes a block. Here is an illustration:
$ irb >> trickyhash = Hash.new({}) => {} >> trickyhash[:a][:a] = 1 => 1 >> trickyhash[:b] => {:a=>1} >> betterhash = Hash.new {|h,k| h[k] = {} } => {} >> betterhash[:a][:a] = 1 => 1 >> betterhash[:b] => {}ruby-prof and KCachegrind are very useful for profiling and performance optimization. We had problems compiling the OS X Darwin Port of KCachegrind, though - you may just want to run it on linux.
- Vine Server and Viewer 3.0 has been released.
Ask for Help
- "QuickSilver for Dummies?" - What is a good resource to learn about QuickSilver?
Standup 01/16/2008
Ask for Help
- "Does Intellij Idea sometimes do an SVN up without asking?"
- Sometimes, if you do an svn up on the command line, IntelliJ will not always pick up the changes. You need to make sure you click the "refresh" button in the version control "Changes" view, not just the "synchronize" button on the main toolbar.
Standup 01/15/2008
Ask for Help
- "Can you use Google Maps on an https page?"
- Probably via an iframe. Is there a preferred way?
ruby-debug in 30 seconds (we don't need no stinkin' GUI!)
Many people (including me) have complained about the lack of a good GUI debugger for Ruby. Now that some are finally getting usable, I've found I actually prefer IRB-style ruby-debug to a GUI.
There's good tutorial links on the ruby-debug homepage, and a very good Cheat sheet, but I wanted to give a bare-bones HOWTO to help you get immediately productive with ruby-debug.
Install the latest gem
$ gem install ruby-debug
Install the cheatsheet
$ gem install cheat
$ cheat rdebug
Set autolist, autoeval, and autoreload as defaults
$ vi ~/.rdebugrc
set autolist
set autoeval
set autoreload
Run Rails (or other app) via rdebug
$ rdebug script/server
Breakpoint from rdebug
(rdb:1) b app/controllers/my_controller.rb:10
Breakpoint in source
require 'ruby-debug'
debugger
my_buggy_method('foo')
Catchpoint
(rdb:1) cat RuntimeError
Continue to breakpoint
(rdb:1) c
Next Line (Step Over)
(rdb:1) n
Step Into
(rdb:1) s
Continue
(rdb:1) c
Where (Display Frame / Call Stack)
(rdb:1) where
List current line
(rdb:1) l=
Evaluate any var or expression
(rdb:1) myvar.class
Modify a var
(rdb:1) @myvar = 'foo'
Help
(rdb:1) h
There are many other commands, but these are the basics you need to poke around. Check the Cheat sheet for details.
This can also be used directly from any IDE that supports input into a running console (such as Intellij Idea).
That should get you started. So, before you stick in another 'p' to debug, try out ruby-debug instead!
The Power of Versions (Monkey Patches Targeted with Friggin Laser Beams!)
We all love to Monkey Patch Rails and other Ruby apps. However, we sometimes want to target these patches to the specific versions where they are needed.
Here's the easiest way to do this, via RubyGem's built-in version requirement support. The version 0.11.0 should indeed be greater than version 0.9.0:
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> Gem::Version::Requirement.new(['> 0.9.0']).satisfied_by?(Gem::Version.new('0.11.0'))
=> true
Notice that you can't do this with string comparison, because with a per-character comparison,1 is not greater than 9:
irb(main):001:0> '0.11.0' > '0.9.0'
=> false
Here's a little class which puts some helper and example methods around this approach (these methods are all in real use for some of our multipart mailer hacks):
module Pivotal
class VersionChecker
def self.current_rails_version_matches?(version_requirement)
version_matches?(Rails::VERSION::STRING, version_requirement)
end
def self.version_matches?(version, version_requirement)
Gem::Version::Requirement.new([version_requirement]).satisfied_by?(Gem::Version.new(version))
end
def self.rails_version_is_below_2?
result = Pivotal::VersionChecker.current_rails_version_matches?('<1.99.0')
result
end
def self.rails_version_is_below_rc2?
Pivotal::VersionChecker.current_rails_version_matches?('<1.99.1')
end
def self.rails_version_is_1991?
Pivotal::VersionChecker.current_rails_version_matches?('=1.99.1')
end
end
end
(note: some angle brackets changed due to code formatting bug)
Here's an example of how you'd use this:
if Pivotal::VersionChecker.rails_version_is_below_2?
# do some backward compatibility stuff
# or handle bugs that have been fixed in Rails > 2
end
Note that this is only possible now that Rails has started using a more sensible strategy for versioning edge gems and improved support for using advanced versioning with RAILS_GEM_VERSION.
For many projects, this may be overkill. It is useful at Pivotal, though, where many various projects may be on different rails versions, but still want to use the latest common core libraries (and monkey patches) without having to upgrade Rails for their app.
This isn't only useful for monkey patching. It can be handy for any library that wants to be backward- or forward-compatible with its dependencies. I've used this approach at Pivotal and on my personal projects to have Continuous Integration automatically run my tests against multiple dependency versions, without having to change anything other than the CI project name:
There are numerous other related topics for discussion in this area, such as the power of versions or the wisdom of freezing, but I'll save those for future posts. Even if you do freeze the trunk of Rails/plugins/gems, since the version is included in the source, this approach should work barring any conflicts with trunk changes since the last release.
Happy Versioning!







