创建: 2018/03/21
方法一:
eval将字符串作为代码执行, 故写在里边
eval("@#{view_name.to_s} = #{get_view_history}") eval("@#{bookmark_name.to_s} = #{get_bookmark_history}") eval("@#{search_name.to_s} = #{get_search_history}")
方法二:
obj.instance_variable_set(name, val), 用在定义的类的实例生成
book = Book.new("Programming Ruby") book.instance_variable_set(:@title, "Programming Ruby 1.9")
参考: http://ref.xaio.jp/ruby/classes/object/instance_variable_set