chazmeyers.com/blog


Slightly better YAMLization of Ruby Exceptions.

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

Consider the following irb session:

Code (ruby)
  1.  
  2. irb(main):001:0> e = Exception.new("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"
  10.  

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

Code (ruby)
  1.  
  2. cpm@juno:~/helpticket-dev/trunk$ cat lib/better_exception_yaml.rb
  3. require ‘yaml’
  4.  
  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
  14.  
  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(Exception.new("JERK").to_yaml).message
  19. => "JERK"
  20.  

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

I’m replacing some backgroundrb workers that do long, slow TCP connections with async eventmachine code in one of my Rails webapps. I ran into some problems installing eventmachine 0.7.2 on FreeBSD 4.8-STABLE with no root access, so I’m documenting how I got around these problems. I’m certain this isn’t the best way to fix this problem, but it worked for me.

First of all, “gem install eventmachine” fails because on FreeBSD, gcc expects “-pthread” when using the pthreads library. Linux gcc normally uses “-lpthread”. Instead, download and unpack the .tar.gz.

You’ll notice that “ruby setup.rb config” fails with the same error as “gem install eventmachine”. Open ext/extconf.rb in the editor of your choice and search for

Code (ruby)
  1.  
  2. unless have_library(‘pthread’)
  3.   exit
  4. end
  5.  

Comment this entire block of code out. It is dead to us. Now when you run “ruby setup.rb config”, a Makefile will be generated. Huzzah! Now, open ext/Makefile and look for a place to add -pthread. I added it to the LIBS environment variable.

Now, you may or may not be able to run “ruby setup.rb setup && ruby setup.rb install” without problems.
If so, great!
If not, I have nothing else to share with you. Best of luck with your problems.

My mind is gone.

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

Today I decided to finally start teaching myself Python. Out of curiosity, I tried the following:

Code (python)
  1.  
  2. >>> class NoneType:
  3. …     def __str__(self):
  4. …         return ""
  5. >>> None
  6. >>> str(None)
  7. ‘None’
  8.  

Is it sick that I find these results disappointing? Has Ruby corrupted my mind?

A few years ago I would have never even thought about overriding methods of a language’s null type.

Double-you-tee-eff, Gmail?

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

I just noticed that if you rename or remove a label, filters don’t get updated appropriately.

For example, let’s say that I don’t like having MySpace or FaceBook notifications in my inbox, so I create a MySpace label and and
FaceBook label and set up filters that archives mail and labels it appropriately.

Let’s skip ahead a few months. I now have filters and labels set up for MySpace, FaceBook, Virb, Friendster, LiveJournal and many other websites. I’m starting to experience label overload. To try to keep things under control, I rename the MySpace label “Social Networking Crap”.

This is what I would expect to happen:

  • I should get some sort of warning that FaceBook, Virb, Friendster, LiveJournal, etc. labels are referenced in filters, so they shouldn’t be deleted.
  • Mail previously filtered under MySpace should notice the name change and now filter under “Social Networking Crap”.

This is what does happen:

  • FaceBook, Virb, Friendster, and LiveJournal mail just gets archived without any labeling. This is sub-optimal in my opinion, but somewhat understandable.
  • Mail previously filtered under MySpace is also archived without any labeling. WTF?

Of course, the real solution in this hypothetical scenario is to not participate in as much social networking crap. But I digress.

Update Did I speak too soon? It seems like sometimes the expected MySpace behavior works, while other times it doesn’t? Odd.