When should I use Struct vs. OpenStruct?

后端 未结 9 1153
孤城傲影
孤城傲影 2020-11-28 00:35

In general, what are the advantages and disadvantages of using an OpenStruct as compared to a Struct? What type of general use-cases would fit each of these?

9条回答
  •  一生所求
    2020-11-28 01:14

    Have a look at the API with regard to the new method. A lot of the differences can be found there.

    Personally, I quite like OpenStruct, as I don't have to define the structure of the object beforehand, and just add stuff as I want. I guess that would be its main (dis)advantage?

    • http://www.ruby-doc.org/core/classes/Struct.html
    • http://www.ruby-doc.org/stdlib/libdoc/ostruct/rdoc/classes/OpenStruct.html

提交回复
热议问题