convert hash to object

后端 未结 6 725
小蘑菇
小蘑菇 2021-02-07 03:06

I am trying to convert hash and nested hashes to objects.

so far first hash object is converted successfully by this code:

class Hashit
  def initialize(         


        
6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-07 03:50

    You can use OpenStruct http://ruby-doc.org/stdlib-2.0.0/libdoc/ostruct/rdoc/OpenStruct.html

    user = OpenStruct.new({name: "Jimmy Cool", age: "25"})
    user.name #Jimmy Cool
    user.age #25
    

提交回复
热议问题