[rspec-users] class << self considered harmful... really?

Avdi Grimm avdi at avdi.org
Wed Nov 26 11:47:29 EST 2008


On Wed, Nov 26, 2008 at 8:23 AM, Shot (Piotr Szotkowski) <shot at hot.pl> wrote:
> If you don't use class << self, you have to work on class variables and
> you can't use the attr_* shorthands to access the singleton's variables.

It's true you need to use class << self to use attr_* macros, but not
true that you can't use singleton instance variables otherwise:

  class Foo
    class << self
      attr_accessor :class_var
    end

    def self.hello
      puts "@class_var: #{@class_var}"
    end
  end

  Foo.class_var = 42
  Foo.hello

=> "@class_var: 42"

-- 
Avdi

Home: http://avdi.org
Developer Blog: http://avdi.org/devblog/
Twitter: http://twitter.com/avdi
Journal: http://avdi.livejournal.com


More information about the rspec-users mailing list