Double escaping backslashes in ruby.
If you have a string that looks like:
\
and you want it to look like:
\\
in ruby, you have to do the following:
-
-
irb(main):070:0> str = "\\"
-
=> "\\"
-
irb(main):071:0> puts str
-
\
-
=> nil
-
irb(main):073:0> puts str.gsub("\\", "\\\\\\\\")
-
\\
-
=> nil
-
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.
Consider the following irb session:
-
-
irb(main):001:0> e = Exception.new("Jerk!")
-
=> #<Exception: Jerk!>
-
irb(main):002:0> e.message
-
=> "Jerk!"
-
irb(main):003:0> require ‘yaml’
-
=> true
-
irb(main):004:0> YAML.load(e.to_yaml).message
-
=> "Exception"
-
That sucks. Where did the “Jerk!” go? YAML ate it. If you include this code, you can keep your “Jerk!”:
-
-
cpm@juno:~/helpticket-dev/trunk$ cat lib/better_exception_yaml.rb
-
require ‘yaml’
-
-
class Exception
-
def Exception.yaml_new( klass, tag, val )
-
o = YAML.object_maker( klass, {})
-
val.each_pair do |k,v|
-
o.instance_variable_set("@#{k}", v)
-
end
-
o.exception(val["message"])
-
end
-
end
-
-
cpm@juno:~/helpticket-dev/trunk$ irb
-
irb(main):001:0> require ‘lib/better_exception_yaml’
-
=> true
-
irb(main):002:0> YAML.load(Exception.new("JERK").to_yaml).message
-
=> "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.
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
-
-
unless have_library(‘pthread’)
-
exit
-
end
-
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.