class JObject < Hash
def method_missing(symbol, *args)
if symbol.to_s[-1] == ?=
self[symbol.to_s[0..-2].to_sym]= args[0]
elsif keys.include?(symbol)
self[symbol]
else
nil
end
end
end
c = JObject.new
c.str = 1
c.dex = 2
c.int = 3
c.def = "4"
p c #{:str=>1, :dex=>2, :int=>3, :def:=4}
p c.str # 1
p c.dex # 2
p c.int # 3
p c.def # "4"
p c.mdf # nil
c.sub = JObject.new
c.sub.r = 5
p c.sub.r # => 5
c.func = Proc.new {p c.str}
c.func.call #=> 1
class JObject < Hash
def method_missing(symbol, *args)
if symbol.to_s[-1] == ?=
self[symbol.to_s[0..-2].to_sym]= args[0]
elsif keys.include?(symbol)
self[symbol]
else
nil
end
end
end
c = JObject.new
c.str = 1
c.dex = 2
c.int = 3
c.def = "4"
p c #{:str=>1, :dex=>2, :int=>3, :def:=4}
p c.str # 1
p c.dex # 2
p c.int # 3
p c.def # "4"
p c.mdf # nil
c.sub = JObject.new
c.sub.r = 5
p c.sub.r # => 5
c.func = Proc.new {p c.str}
c.func.call #=> 1