The following:
require 'ostruct'
class Entity < OpenStruct
def initialize
super
@context = "instance var"
end
end
entity = Entity.new
pp entity.context
pp entity.instance_variable_get("@context")
entity.context = "custom attribute"
pp entity.context
pp entity.instance_variable_get("@context")
will output:
nil
"instance var"
"custom attribute"
"instance var"
However, adding this lines:
require 'ostruct'
class Entity < OpenStruct
attr_accessor :context # added
def initialize
super
@context = "instance var"
end
end
entity = Entity.new
pp entity.context
pp entity.instance_variable_get("@context")
entity.context = "custom attribute"
pp entity.context
pp entity.instance_variable_get("@context")
will output:
"instance var"
"instance var"
"custom attribute"
"custom attribute"
There is a collision between instance variables and OpenStruct custom attributes, of course.
I propose to change to:
class Entity
attr_accessor :context
def initialize
@attributes = OpenStruct.new
@context = "instance var"
end
end
@mehmetc do you agree?
The following:
will output:
However, adding this lines:
will output:
There is a collision between instance variables and OpenStruct custom attributes, of course.
I propose to change to:
@mehmetc do you agree?