How to define class variables inside class_eval block? I have the following:
module Persist
def self.included(base)
base.class_eval do
@@collection = Connection.new.db('nameofdb').collection(self.to_s.downcase)
def self.get id
end
end
end
def find
@@collection.find().first
end
end
class User
include Persist
end
class Post
include Persist
end
The User and Post classes are both shown :getin introspection with User.methodsor Post.methods. This makes sense as they are defined in the context of class_eval and exactly what I need. Similarly, a method is :findshown as an instance_method of individual classes.
However, what I considered a class variable, i.e. @@collectionturns out to be class_variable level. When I browse User.class_variablesor Post.class_variables, they become empty. However, it Persist.class_variablesshows :@@collection.
? class_eval . , @@collection , ?
, @@collection , . "", "". , , , . ?
, , @@collection.