returning struct data as a hash in ruby

后端 未结 4 1538
我在风中等你
我在风中等你 2021-02-18 18:05

Is there a valid reason for there not being a method to return the data of a standard ruby struct as a hash (member, value pairs)? Seeing that structs and hashes have very simil

4条回答
  •  面向向阳花
    2021-02-18 18:35

    The accepted answer did not work for me, i used the following instead

    require 'ostruct'
    require 'date'
    
    lid = OpenStruct.new(:status=>0,:rowversion=>0,:cre_dt=>DateTime.now.to_date,:cre_user=>'9999999')
    p Hash[lid.each_pair.to_a] #=> {}
    p lid.marshal_dump #=>{:status=>0, :rowversion=>0, :cre_dt=>#, :cre_user=>"9999999"}
    

提交回复
热议问题