tags:

views:

21

answers:

1

I have a Module with a constant and variable.

I wonder how I could include these in a class?

module Software
  VAR = 'hejsan'

  def exit
    @text = "exited"
    puts @text
  end
end

class Windows
  extend Software
  def self.start
    exit
    puts VAR
    puts @text
  end
end

Windows.start

Is this possible?

Thanks!

+1  A: 

Doing exactly what you want is not possible. Instance variables are strictly per object.

This happens to do what you expect, but @text is set on Windows not Software.

module Software
  VAR = 'hejsan'

  def exit
    @text = "exited"
    puts @text
  end
end

class Windows
  class <<self
    include Software
    def start
      exit
      puts VAR
      puts @text
    end
  end
end

Windows.start
taw
what about the constant?
never_had_a_name
`include Software` like in my example will do it. It will only be included in metaclass - so you can use `VAR` from class methods but not instance methods. Is this what you want?
taw