Home > Blockchain >  Remove double quotes from values in hash in ruby
Remove double quotes from values in hash in ruby

Time:04-25

{"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]
}

CodePudding user response:

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
  • Related