Home > Software design >  ruby on rails replace single-quotes with double-quote in a string
ruby on rails replace single-quotes with double-quote in a string

Time:11-12

I am trying to replace a single-quote with double quotes inside a string, as following:

current_res = 25
lowest_res = 15
str = "The result of the child is '#{current_res}' and the lowest grade is '#{lowest_res }'."

I need the output to look like:

str = The result of the child is "25" and the lowest grade is "15".

I tried different method using gsub but nothing work so far. Any ideas?

CodePudding user response:

If that's the only case you're covering where you need to show some output in double quoted string then. How about something simple like following

str = "The result of the child is \"#{current_res}\" and the lowest grade is \"#{lowest_res }\" ."

You can escape quotes in double quoted strings.

  • Related