Class-level accessors in Ruby

While looking at the source code for Capistrano today, I came across a nice idiom for making class-level accessors:

1
2
3
4
5
6
7
8
9
10
11
12
class Actor  
  class << self
    attr_accessor :connection_factory
    attr_accessor :command_factory
    attr_accessor :transfer_factory
    attr_accessor :default_io_proc
  end

  self.connection_factory = DefaultConnectionFactory
  self.command_factory = Command
  self.transfer_factory = Transfer
end

Now you can use these class-level accessors:

1
2
3
Actor.connection_factory
Actor.command_factory
...
Advertisements

Pages

Categories

Copyright ©2008-2013 James E Orchard-Hays, All Rights Reserved

%d bloggers like this: