Is there a way to get the name of the instance we created of a specific class in a class method?
This is what i'm trying to do:
module MyObjectStore
values = {}
temp= {}
define_method :add_check_attribute do |method,*args|
if method.to_s =~ /=$/
temp[method[0..-2]] = args[0]
else
instance_variable_get("@#{method}")
end
end
define_method :method_missing do |method,*args|
add_check_attribute(method,*args)
end
define_method :save do
temp.each {|key,value| instance_variable_set("@#{key}",value)}
values[self] = temp
end
end
class C
include MyObjectStore
end
a = C.new
a.id = 1
a.name = 'gaurav'
a.save
On the third-to-last line of the module, I'm trying to store the values of the object into the new hash having the instance name as the key.
Is anything like that possible?
I'm using self
now, but it gives the whole object, not the instance name.