Slightly better YAMLization of Ruby Exceptions.

Posted in Uncategorized, ruby by chazmeyers on the July 10th, 2007

Consider the following irb session:

Code (ruby)
  2. irb(main):001:0> e ="Jerk!")
  3. => #<Exception: Jerk!>
  4. irb(main):002:0> e.message
  5. => "Jerk!"
  6. irb(main):003:0> require ‘yaml’
  7. => true
  8. irb(main):004:0> YAML.load(e.to_yaml).message
  9. => "Exception"

That sucks. Where did the “Jerk!” go? YAML ate it. If you include this code, you can keep your “Jerk!”:

Code (ruby)
  2. cpm@juno:~/helpticket-dev/trunk$ cat lib/better_exception_yaml.rb
  3. require ‘yaml’
  5. class Exception
  6.   def Exception.yaml_new( klass, tag, val )
  7.     o = YAML.object_maker( klass, {})
  8.     val.each_pair do |k,v|
  9.       o.instance_variable_set("@#{k}", v)
  10.     end
  11.     o.exception(val["message"])
  12.   end
  13. end
  15. cpm@juno:~/helpticket-dev/trunk$ irb
  16. irb(main):001:0> require ‘lib/better_exception_yaml’
  17. => true
  18. irb(main):002:0> YAML.load("JERK").to_yaml).message
  19. => "JERK"

It still sucks because the backtrace is not preserved, but that’s because Exception#to_yaml doesn’t serialize that information. YAML was storing the message, but by default it stores it into a @mesg instance variable instead. Quite odd.

I wasn’t inclined enough to read up on the right way to yamlize new data. Maybe later I’ll add backtracing.

Installing eventmachine on older FreeBSD’s.

Posted in Uncategorized, ruby, RubyOnRails by chazmeyers on the July 5th, 2007

My mind is gone.

Posted in Uncategorized by chazmeyers on the April 17th, 2007

Double-you-tee-eff, Gmail?

Posted in Uncategorized by chazmeyers on the March 21st, 2007