Chillibear
A place for notes about stuff

Ruby

Ruby daemons and at_exit

Silly mistake on my part, but took me a while before I saw the obvious. If you’re writing a daemon in Ruby using the daemons gem (http://daemons.rubyforge.org) and want to have some code run when your script exits (is stopped by the daemon control), then your first thoughts would be to use the at_exit construct. [...]

Strip first character using Ruby

It’s easy enough to remove the last character of a string in Ruby, but what about the first? Probably the neatest way is to just extend the String class with two new method thus: class String def chopfirst self[1..-1] end def chopfirst! self.slice!(0) return self end end The first returns the newly shortened string, but [...]

Ruby: file-tail: `gem_original_require’: no such file to load

Just been trying to play with the file-tail Gem for Ruby and I kept hitting the same error. #!/usr/bin/env ruby require ‘rubygems’ require ‘file-tail’ filename = ‘/tmp/samplefile.txt’ File.open(filename) do |log| log.extend(File::Tail) log.interval = 10 log.backward(10) log.tail { |line| puts line } end when run would produce /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `gem_original_require’: no such file to load — file-tail [...]

Rails test database migrations

One problem it took me ages to spot was that my test database wasn’t up to date with my migrations and therefore my tests kept failing for no obvious reason! So remember to: rake db:test:prepare To ensure the test database is up to date with migrations, etc.

Boolean conditions in Rails find statements

I was having trouble getting a condition set on a standard find query. I had a table with a boolean column and was expecting to do something like this a = model.find(:all, :conditions => ‘checked = true’) But that (and variations on it) didn’t work, a bit of searching turned up a suitable solution here: [...]

After →