'Remove double quotes from values in hash in ruby

{"user_id"=> "row[:user_id]",
 "name" => "row[:name]",
 "address" => "row[:address]"
}

Want to remove the double quotes in values

desired hash

{"user_id"=> row[:user_id],
 "name" => row[:name],
 "address" => row[:address]
}


Solution 1:[1]

Try this here, there are two reg-ex. gsub will do a global substitution.

I did it with the help of https://regex101.com/

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 KargWare