[Ruby]如何动态创建一个对象

这个功能要模拟Groovy中Expando和Javascript中的{},这里要用到ruby自带的osstruct库

#!C:/Ruby192/bin/ruby.exe

require 'ostruct'

def mytest 
puts "a method"
end 
o = OpenStruct.new
o.method1 = mytest
o.prop1 = "a property"
puts o.method1
puts o.prop1
 

  

原文地址:https://www.cnblogs.com/buhaiqing/p/2822625.html