Eigenclass: why this syntax?

Hi. Consider this syntax:

  module Foo
    class << self
      attr_accessor :use_colours
    end
  end

Now, it works to do this:

Foo.use_colours = true
puts Foo.use_colours

Foo.use_colours = false
puts Foo.use_colours

My questions:

- Why is the syntax "class << self" used? For an array, << means to
append. I am unsure what this means for class, or why this syntax was
used.

- Also, in the above example, is it possible to query the @use_colours
variable for when instance methods of module Foo are mixed into a class?
I would like to mixin instance variables that are defined on the
instance level of the module, inside methods defined in that module. So
yeah, basically I want to use a module like a class, but it seems ruby
gives obstacles to that attempt.

···

--
Posted via http://www.ruby-forum.com/.

Not sure I understand the question, but it sounds like you want this:

module A
  attr_accessor :whatev
end

# include it in the singleton class
class B
  class << self
    include A
  end
  self.whatev = 'something'
  whatev # => "something"
end

# shorthand version of the above
class C
  extend A
  self.whatev = 'something'
  whatev # => "something"
end

···

On Wed, Apr 3, 2013 at 7:36 PM, Marc Heiler <lists@ruby-forum.com> wrote:

- Also, in the above example, is it possible to query the @use_colours
variable for when instance methods of module Foo are mixed into a class?
I would like to mixin instance variables that are defined on the
instance level of the module, inside methods defined in that module. So
yeah, basically I want to use a module like a class, but it seems ruby
gives obstacles to that attempt.

I can't speak for why this particular notation was used, obviously,
but the '<<' operation is used similarly in other places as well, not
just appending to an array. I look at it as basically a generic
append; in the case of the class's eigenclass, you're appending
something to it (perhaps creating the eigenclass in the first place).

···

On Wed, Apr 3, 2013 at 7:36 PM, Marc Heiler <lists@ruby-forum.com> wrote:

- Why is the syntax "class << self" used? For an array, << means to
append. I am unsure what this means for class, or why this syntax was
used.