ruby hash autovivification (facets)

后端 未结 3 842
予麋鹿
予麋鹿 2020-12-09 21:16

Here is a clever trick to enable hash autovivification in ruby (taken from facets):

  # File lib/core/facets/hash/autonew.rb, line 19
  def self.autonew(*arg         


        
3条回答
  •  被撕碎了的回忆
    2020-12-09 22:17

    Alternatively, you might consider my xkeys gem. It's a module that you can use to extend arrays or hashes to facilitate nested access.

    If you look for something that doesn't exist yet, you get a nil value (or another value or an exception if you prefer) without creating anything by looking. It can also append to the end of arrays.

    You can opt to autovivify either hashes or arrays for integer keys (but just once for the entire structure).

提交回复
热议问题