ruby - 如果 MAP 中存在密钥,则修改参数
问题描述
仅当 MAP 中存在密钥时,我才想修改此哈希。我试过这个:
FIXED_COUNTRY_TO_PHONE = {
'FI' => '+358501111',
'RU' => '4019900780',
'SE' => '+4672345678',
'UA' => '0123456789',
'KZ' => '0123456789'
}.freeze
def base_params_for(payment_type)
{ payment_type: payment_type,
currency: currency,
country: country,
amount: amount_for(currency) }
params[:customer_phone] = FIXED_COUNTRY_TO_PHONE[country] if FIXED_COUNTRY_TO_PHONE.has_key? 'FI'
end
:customer_phone
仅当密钥存在时,我如何才能修改参数?
解决方案
你可以tap
在这里使用:
{
payment_type: payment_type,
currency: currency,
country: country,
amount: amount_for(currency)
}.tap do |params|
if FIXED_COUNTRY_TO_PHONE.has_key?(country)
params[:customer_phone] = FIXED_COUNTRY_TO_PHONE[country]
end
end
推荐阅读
- javascript - 将 VSCode 更新到 1.32.1 后开玩笑“未找到测试”
- python - 如何用名称腌制熊猫数据框
- c++ - Android Studio 找不到对 make_unique 的引用,但项目编译正常
- function - 如何将选择下拉类别列表添加到主题选项
- android - 显示从图库中选择的多个图像
- ubuntu - 如何找到我下载的 linux-source-4.15.0?
- javascript - 为什么我在 node.js 中使用 sendFile() 方法为两个路由提供静态文件时会出错
- maven - 如何在 Maven 中包含 gradle 项目
- sql - 合并两个 SQL 查询 - 无法在 postgres 中将 WHERE 子句与 between 选项一起使用
- javascript - 角度 7 未按要求发送标头