[prev in list] [next in list] [prev in thread] [next in thread] 

List:       ruby-talk
Subject:    Re: class methods like attr:
From:       "Timothy Goddard" <interfecus () gmail ! com>
Date:       2006-01-24 15:37:56
Message-ID: 1137887771.559969.161320 () g49g2000cwa ! googlegroups ! com
[Download RAW message or body]

Here's a quick hack. There are probably better ways.

def self.var_with_default(variable, default)
  @@current_default = default
  class_eval <<-END_BLOCK
    @@default_#{variable.to_s} = @@current_default
    attr_writer #{variable.to_s}
    def #{variable.to_s}
      @#{variable.to_s} || @@default_#{variable.to_s}
    end
  END_BLOCK
end

Include that in the class it will be used in (or an ancestor). This
method is not thread safe.


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic