chazmeyers.com/blog


Double escaping backslashes in ruby.

Posted in ruby by chazmeyers on the July 11th, 2007

If you have a string that looks like:
\

and you want it to look like:
\\

in ruby, you have to do the following:

Code (ruby)
  1.  
  2. irb(main):070:0> str = "\\"
  3. => "\\"
  4. irb(main):071:0> puts str
  5. \
  6. => nil
  7. irb(main):073:0> puts str.gsub("\\", "\\\\\\\\")
  8. \\
  9. => nil
  10.  

In Ruby, you can use perl-style regular expression numbered group references in replacement strings (ie \\1 = the first captured group of the regexp). So, to make a literal backslash in gsub, you need “\\\\”. And to make the second one, you need “\\\\\\\\”.

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.