Deep set

作者: n5ken | 来源:发表于2019-10-24 14:01 被阅读0次

    hash = { }
    def deep_set(hash, value, keys)
    keys[0...-1].inject(hash) do |acc, h|
    v = acc.public_send(:[], h)
    v ? v : acc[h] = {}
    end.public_send(:[]=, keys.last, value)
    end

    deep_set(hash, 42, [:a, :b, :c])
    deep_set(hash, 43, [:a, :b, :d])
    deep_set(hash, 44, [:a, :b, :e])
    puts hash

    相关文章

      网友评论

          本文标题:Deep set

          本文链接:https://www.haomeiwen.com/subject/senrvctx.html