I’m writing a program like this:
module Filter
def self.included?(klass)
@count = 0
end
end
class Object
include Filter
end
class Person
def get_count
puts @count
end
end
I want to define an instance variable @count through mixing Filter, hoping this mixin is accessible to all the Object.
However, my way doesn’t work.
Can anyone tell me how to achieve this?
Don’t use a variable, use an attribute:
Also, don’t include stuff into
Object, only include in your classes. Polluting the main namespace with non-general methods is bad and could lead to method name clashes in other classes, causing hard-to-find bugs.Only include modules where you think they’re needed, not everywhere.