我不知道发生了什么,但我有两个哈希值:first_hash
{
"SspFileIds"=>[
"0823c9bf-d41e-4324-80be-3fb2c6ae49f0"
],
"SendEnvelopeDescription"=>
{
"CallbackUrl"=>"https://test.com?envelope=##EnvelopeId##&action=##Action##&event=##EventType##&recipient=##RecipientEmail##",
"WorkstepEventCallback"=>{
"Url"=>"https://test.com?envelope=##EnvelopeId##&action=##Action##&event=##EventType##&recipient=##RecipientEmail##",
"WhiteList"=>[
"string"
]
},
"Steps"=>
[
{
"OrderIndex"=>1,
"Recipients"=>[
{
"Email"=>"teddy@olson.info",
"FirstName"=>"Merle",
"LastName"=>"Stracke",
"LanguageCode"=>"en"
}
]
},
{
"OrderIndex"=>1,
"Recipients"=>[
{
"Email"=>"zora_gerhold@lemke.co",
"FirstName"=>"Laine",
"LastName"=>"Boehm",
"LanguageCode"=>"en"
}
]
}
]
}
}
第二个问题:second_hash
{
"SspFileIds"=>[
"7fcf6021-386d-4f31-a871-a89afb8fb36e"
],
"SendEnvelopeDescription"=>{
"Name"=>"1 Recipient",
"EmailSubject"=>"Please sign the enclosed envelope",
"EmailBody"=>"Dear #RecipientFirstName# #RecipientLastName#\n\n#PersonalMessage#\n\nPlease sign the envelope #EnvelopeName#\n\nEnvelope will expire at #ExpirationDate#",
"DisplayedEmailSender"=>"",
"EnableReminders"=>true,
"FirstReminderDayAmount"=>5,
"RecurrentReminderDayAmount"=>3,
"BeforeExpirationDayAmount"=>3,
"DaysUntilExpire"=>28,
"StatusUpdateCallbackUrl"=>"",
"LockFormFieldsAtEnvelopeFinish"=>false,
"Steps"=>[
{
"OrderIndex"=>1,
"Recipients"=>[
{
"Email"=>"Placeholder:",
"FirstName"=>"",
"LastName"=>"",
"LanguageCode"=>"",
"DisableEmail"=>false,
"AddAndroidAppLink"=>false,
"AddIosAppLink"=>false,
"AddWindowsAppLink"=>false,
"AllowDelegation"=>true,
"AllowAccessFinishedWorkstep"=>false,
"SkipExternalDataValidation"=>false,
"AuthenticationMethods"=>[],
"IdentificationMethods"=>[]
}
],
"RecipientType"=>"Signer",
}],
}
}
现在我想把first_hash
添加到second_hash
中,只覆盖两者中存在的键/值,并保持second_hash中的其余部分不变(应该添加到结果中)。test = first_hash.merge(second_hash)
但结果是令人惊讶的-什么都没有改变,它打印第二哈希不变:
> test
{"SspFileIds"=>["7fcf6021-386d-4f31-a871-a89afb8fb36e"],
"SendEnvelopeDescription"=>
{"Name"=>"1 Recipient",
"EmailSubject"=>"Please sign the enclosed envelope",
"EmailBody"=>"Dear #RecipientFirstName# #RecipientLastName#\n\n#PersonalMessage#\n\nPlease sign the envelope #EnvelopeName#\n\nEnvelope will expire at #ExpirationDate#",
"DisplayedEmailSender"=>"",
"EnableReminders"=>true,
"FirstReminderDayAmount"=>5,
"RecurrentReminderDayAmount"=>3,
"BeforeExpirationDayAmount"=>3,
"DaysUntilExpire"=>28,
"StatusUpdateCallbackUrl"=>"",
"LockFormFieldsAtEnvelopeFinish"=>false,
"Steps"=>
[{"OrderIndex"=>1,
"Recipients"=>
[{"Email"=>"Placeholder:",
"FirstName"=>"",
"LastName"=>"",
"LanguageCode"=>"",
"DisableEmail"=>false,
"AddAndroidAppLink"=>false,
"AddIosAppLink"=>false,
"AddWindowsAppLink"=>false,
"AllowDelegation"=>true,
"AllowAccessFinishedWorkstep"=>false,
"SkipExternalDataValidation"=>false,
"AuthenticationMethods"=>[],
"IdentificationMethods"=>[]}],
"RecipientType"=>"Signer"}]}}
型
怎么回事,我是不是误解了什么?我用的是Rails 7和Ruby 3。
[编辑]
expected_result
{
'SspFileIds' => [
'0823c9bf-d41e-4324-80be-3fb2c6ae49f0',
],
'SendEnvelopeDescription' => {
'CallbackUrl' => 'https://test.com?envelope=##EnvelopeId##&action=##Action##&event=##EventType##&recipient=##RecipientEmail##',
'WorkstepEventCallback' => {
'Url' => 'https://test.com?envelope=##EnvelopeId##&action=##Action##&event=##EventType##&recipient=##RecipientEmail##',
'WhiteList' => [
'string',
],
},
'Name' => '1 Recipient',
'EmailSubject' => 'Please sign the enclosed envelope',
'EmailBody' => "Dear #RecipientFirstName# #RecipientLastName#\n\n#PersonalMessage#\n\nPlease sign the envelope #EnvelopeName#\n\nEnvelope will expire at #ExpirationDate#",
'DisplayedEmailSender' => '',
'EnableReminders' => true,
'FirstReminderDayAmount' => 5,
'RecurrentReminderDayAmount' => 3,
'BeforeExpirationDayAmount' => 3,
'DaysUntilExpire' => 28,
'StatusUpdateCallbackUrl' => '',
'LockFormFieldsAtEnvelopeFinish' => false,
'Steps' => [
{
'OrderIndex' => 1,
'Recipients' => [
{
'Email' => 'teddy@olson.info',
'FirstName' => 'Merle',
'LastName' => 'Stracke',
'LanguageCode' => 'en',
},
],
'RecipientType' => 'Signer',
},
],
},
}
1条答案
按热度按时间vfh0ocws1#
您是否尝试过使用
deep_merge
?https://apidock.com/rails/Hash/deep_merge你想要的是
test = first_hash.deep_merge(second_hash)