0

In my Rails application, I got a concern which is included within a base class. The base class is then inherited.

The included module defines a class variable for which I'd like to have the same value whether or not I'm in the base class or child class.

The code looks like something similar:

module M

  def self.included(base)
    base.extend ClassMethods
  end

  module ClassMethods

    attr_accessor :lol

    def toto
      @lol ||= {}
    end

    def toto=(val)
      @lol = val
    end

  end

end


class A
  include M
end

class B < A
end

puts A::toto
puts B::toto
puts A::toto = 12
puts B::toto

I'm actually using concerns, but the behavior is the same.

This code prints

{}
{}
12
{}

while I'd like

{}
{}
12
12

Is there any wa to achieve that? I've tried different combination of attr_accessor / cattr_accessor, but nothing worked.

1 Answer 1

2

Try the following:

module M
  def self.included(base)
    base.extend ClassMethods
    base.instance_eval do
      cattr_accessor :toto, instance_writer: false, instance_reader: false do 
        {}
      end          
    end  
  end

  module ClassMethods
    # class methods 
  end
end


class A
  include M
end

class B < A
end

puts A.toto
puts B.toto
puts A.toto = 12
puts B.toto

# {}
# {}
# 12
# 12
Sign up to request clarification or add additional context in comments.

1 Comment

Excellent, exactly what I was looking for! Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.