Ruby mixin class methods

I’m attempting to provide a class attribute setter and getter via a mixin, but clearly I’m doing something fundamentally wrong.

This code results in the error mod.rb:16:in '<class:Klass>': undefined method 'set_n' for Klass:Class (NoMethodError):

module Mixin
  @@n = 0

  def self.set_n(n)
    @@n = n
  end

  def self.get_n
    @@n
  end
end


class Klass
  include Mixin
  set_n 100
end


n = Klass.get_n

Little help?

%d bloggers like this: