Tag Archive for 'ruby'

Ruby En Rails 2007 Conference

On June 7th the Ruby En Rails 2007 Conference will be held here in Amsterdam. I’m really looking forward to meeting up with the dutch Rails crowd. I’ll be giving a lighting talk on Mongrel clustering and will share my experience with this in use on a busy site. Also looking forward to some of the talks, especially the opening keynote. Hope to see you there!!

Update: I just read there are no more seats left, so hope you got in!

Using MPlayer identify with Ruby

For a project I need to fetch some metadata from WAV recordings. Mplayer can extract this information nicely with the -identify command. I’m using Ruby Sessions for executing the external mplayer command, although a simple IO.POpen() might do fine too. The code wrapper class looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
require 'rubygems'
require_gem 'session'

class MediafileInfo
        MPLAYER_BINARY = "mplayer"
        MPLAYER_IDENTIFY = "-identify -vo null -ao null -frames 0"

        def initialize(filename)
                stdout, stderr = '', ''
                shell = Session::Shell.new
                shell.execute "#{MPLAYER_BINARY} #{MPLAYER_IDENTIFY} #{filename}", :stdout => stdout, :stderr => stderr

                vars = (stdout.split(/\n/).collect! { |o| o if o =~ /^ID_/ } ).compact!

                vars.each { |v|
                        a, b = v.split("=")
                        eval "@#{a.to_s.downcase} = \"#{b}\""
                }
        end

        # Intercept calls
        def method_missing(method_name, *args)
                value = eval "@id_#{method_name.to_s.downcase}"
        end
end



All ID_* lines that mplayer spits out will now be method calls (and instance variables) of your MediafileInfo object. The method_missing call is there to do proper value defaulting if the variable doesn’t exist, but I’m not using it as such right now. To get the playing time of every type of mediafile that mplayer understands you simply do someting like:

1
2
3
info = MediafileInfo.new("voicemail.wav")

puts "Voicemail playtime: #{info.length.to_i} seconds"

Ruby 0wnz! :)


Rails’ end_of_month

Whilst building the payment module for one of my Rails apps I used the very convenient Rails’ Time extension method end_of_month returns the last day of the month. However it sets the time to 00:00:00, which is quite unexpected IMHO! Technically the end of the month is still 86400 seconds away. This behaviour is documented, but if you haven’t actually read the whole API it might cause some nasty surprises. I fixed it by simply adding 86399 seconds to end_of_time. That still leaves 999 usecs, but I can live with that :)

ActiveRecord InvalidStatement

It seems that ActiveRecord cannot return sane errors when some constraint on a database operation is not met, for instance if you forget to set an attribute which has a “NOT NULL” constraint. If this happens AR simply throws an InvalidStatement exception. You would have to actually search your database logs for the error message. This really sucks! Also check out this blog post on the same topic. Oh well, perhaps I should try and fix this issue instead of griping, it’s open source after all. You can of course also argue that the model file in the Rails app was not complete (:allow_nil => false)..