加入我们,或者,欢迎回来。
您需要 登录 才可以下载或查看,没有帐号?注册会员  
 
x
 
 本帖最后由 SailCat 于 2018-1-20 19:24 编辑  
 
其实我也不知道能有啥用…… 
而且ruby没有undefined(熟悉js的应该知道这和null不太一样)只好用nil了 
 
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  
 
  
 
 |