从 ruby 中的散列值中删除双引号
Remove double quotes from values in hash in ruby
{"user_id"=> "row[:user_id]",
"name" => "row[:name]",
"address" => "row[:address]"
}
想要删除值中的双引号
所需的哈希值
{"user_id"=> row[:user_id],
"name" => row[:name],
"address" => row[:address]
}
在这里试试这个,有两个 reg-ex。 gsub
将进行全局替换。
的帮助下完成的
str = '{"user_id"=> "row[:user_id]",
"name" => "row[:name]",
"address" => "row[:address]"
}'
re = /\"row\[:/m
subst = 'row[:'
result1 = str.gsub(re, subst)
re = /]"/m
subst = ']'
result2 = result1.gsub(re, subst)
# Print the result of the substitution
puts result2
{"user_id"=> "row[:user_id]",
"name" => "row[:name]",
"address" => "row[:address]"
}
想要删除值中的双引号
所需的哈希值
{"user_id"=> row[:user_id],
"name" => row[:name],
"address" => row[:address]
}
在这里试试这个,有两个 reg-ex。 gsub
将进行全局替换。
str = '{"user_id"=> "row[:user_id]",
"name" => "row[:name]",
"address" => "row[:address]"
}'
re = /\"row\[:/m
subst = 'row[:'
result1 = str.gsub(re, subst)
re = /]"/m
subst = ']'
result2 = result1.gsub(re, subst)
# Print the result of the substitution
puts result2