ruby 用另一个密钥替换所有HASH密钥

dojqjjoe  于 2023-10-17  发布在  Ruby
关注(0)|答案(3)|浏览(120)

我尝试用另一个键替换哈希中的所有键,像这样:

{
  name: 'Josh',
  surname: 'Simon',
  cars: [{
    name: 'Diablo',
    seats: [{ name: 'Josh', surname: 'Simon' }],
  },
  {
    name: 'Raptor',
    seats: [{ name: 'Josh', surname: 'Simon' }],
  },
  {
    name: 'Testarossa',
    seats: [{ name: 'Josh', surname: 'Simon' }],
  }],
}

假设我想把汽车的钥匙改为,这样我就可以实现这样的功能

{
  name: 'Josh',
  surname: 'Simon',
  cars: [{
    name: 'Diablo',
    people: [{ name: 'Josh', surname: 'Simon' }],
  },
  {
    name: 'Raptor',
    people: [{ name: 'Josh', surname: 'Simon' }],
  },
  {
    name: 'Testarossa',
    people: [{ name: 'Josh', surname: 'Simon' }],
  }],
}

这能以某种方式做到吗?

lf3rwulv

lf3rwulv1#

您可以删除旧的键并将该值分配给新的键

hash =
  {
    name: 'Josh',
    surname: 'Simon',
    cars: [{
      name: 'Diablo',
      seats: [{ name: 'Josh', surname: 'Simon' }],
    },
    {
      name: 'Raptor',
      seats: [{ name: 'Josh', surname: 'Simon' }],
    },
    {
      name: 'Testarossa',
      seats: [{ name: 'Josh', surname: 'Simon' }],
    }],
  }
hash[:cars].each { |h| h[:people] = h.delete(:seats) }
hash

# => {
#      name: 'Josh',
#      surname: 'Simon',
#      cars: [{
#        name: 'Diablo',
#        people: [{ name: 'Josh', surname: 'Simon' }],
#      },
#      {
#        name: 'Raptor',
#        people: [{ name: 'Josh', surname: 'Simon' }],
#      },
#      {
#        name: 'Testarossa',
#        people: [{ name: 'Josh', surname: 'Simon' }],
#      }],
#    }
envsm3lx

envsm3lx2#

如果变量hash保存了示例中给出的哈希值,并且该对象将被改变,则可以编写以下内容。

hash[:cars].each { |h| h.transform_keys! { |k| k == :seats ? :people : k } }
  #=> [
  #    {
  #     :name=>"Diablo",
  #     :people=>[{:name=>"Josh", :surname=>"Simon"}]
  #    },
  #    {
  #     :name=>"Raptor",
  #     :people=>[{:name=>"Josh", :surname=>"Simon"}]
  #    },
  #    {
  #     :name=>"Testarossa",
  #     :people=>[{:name=>"Josh", :surname=>"Simon"}]
  #    }
  #   ]

所以现在

hash
  #=> {
  #    :name=>"Josh",
  #    :surname=>"Simon",
  #    :cars=>[
  #      {
  #       :name=>"Diablo",
  #       :people=>[{:name=>"Josh", :surname=>"Simon"}]
  #      },
  #      {
  #       :name=>"Raptor",
  #       :people=>[{:name=>"Josh", :surname=>"Simon"}]
  #      },
  #      {
  #       :name=>"Testarossa",
  #       :people=>[{:name=>"Josh", :surname=>"Simon"}]
  #      } 
  #    ]
  #   }

请参见Hash#transform_keys!.
如果散列不被改变,你可以在一个深副本上操作。方法之一是使用Marshal::load和Marshal::dump:

h = Marshal.load(Marshal.dump(hash))
  #=> {
  #    :name=>"Josh",
  #    :surname=>"Simon",
  #    :cars=>[
  #      {
  #       :name=>"Diablo",
  #       :seats=>[{:name=>"Josh", :surname=>"Simon"}]
  #      },
  #      {
  #       :name=>"Raptor",
  #       :seats=>[{:name=>"Josh", :surname=>"Simon"}]
  #      },
  #      {
  #        :name=>"Testarossa",
  #        :seats=>[{:name=>"Josh", :surname=>"Simon"}]
  #      }
  #    ]
  #   }

请注意,如果我们通过写入来更改h,例如,

h[:name] = "Lola"
h[:cars][1][:seats][0] = "cats"

我们可以验证hash没有改变。

sg3maiej

sg3maiej3#

你可以这样做:

hash =
  {
    name: 'Josh',
    surname: 'Simon',
    cars: [{
      name: 'Diablo',
      seats: [{ name: 'Josh', surname: 'Simon' }],
    },
    {
      name: 'Raptor',
      seats: [{ name: 'Josh', surname: 'Simon' }],
    },
    {
      name: 'Testarossa',
      seats: [{ name: 'Josh', surname: 'Simon' }],
    }],
  }

hash[:cars].each { |record| record.store(:people, record.delete(:seats)) }

相关问题