Ruby on Rails
RubyIdiomsThatFailInRails

This is just a scratch page

Someday it may grow into a useful resource; for the moment it’s just a list:

Hash defaults

In Ruby you can write something like


class An_appointment_book
    def initialize
        @schedule = Hash.new { Hash.new { Hash.new { Array.new } } }
        end
    def add_to_scedule(year,month,day,event)
        @schedule[year][month][day] << event
        end
    end

In rails this is often not possible because there is no way to serialize a Hash with a default procedure, so you wind up having to write the slightly more cluttered:


class An_appointment_book
    def initialize
        @schedule = {}
        end
    def add_to_scedule(year,month,day,event)
        (((@schedule[year] ||= {})[month] ||= {})[day] ||= []) << event
        end
    end

case x; when Array…


case x
    when Array then print "Array\n"
    else
        print "Bogus\n" if x.is_a? Array
    end

Sometimes prints bogus (due to what I see as a bug in association_proxy.rb).