Collecting hashes into OpenStruct creates “table” entry

前端 未结 6 973
滥情空心
滥情空心 2021-02-04 05:46

Why this (evaluated in Rails console)

[{:a => :b}].collect {|x| OpenStruct.new(x)}.to_json

adds a \"table\" record in there?



        
6条回答
  •  天命终不由人
    2021-02-04 05:46

    With ruby 2.1.2 you can use the following to get JSON without the table root element:

    [{:a => :b}].collect {|x| OpenStruct.new(x).to_h}.to_json
     => "[{\"a\":\"b\"}]"
    

提交回复
热议问题