When is it better to use a Struct rather than a Hash in Ruby?

前端 未结 6 1281
清歌不尽
清歌不尽 2021-01-30 20:44

A Ruby Struct allows an instance to be generated with a set of accessors:

# Create a structure named by its constant
Customer = Struct.new(:name, :address)     #         


        
6条回答
  •  轮回少年
    2021-01-30 20:53

    It's mainly performance. Struct is much faster, by order of magnitudes. And consumes less memory when compared to Hash or OpenStruct. More info here: When should I use Struct vs. OpenStruct?

提交回复
热议问题