Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby - How to output "#{}" in a string?

Tags:

string

ruby

Ruby uses the following characters #{} for string interpolation. If, for some reason, I would like to output those characters in a string, how would I do so?

like image 795
Marco Lau Avatar asked Apr 23 '26 06:04

Marco Lau


2 Answers

Just use single quote:

puts '#{}'

Or if you want to use double quote, then escape it.

puts "\#{some_var} will be #{some_var}"
like image 111
xdazz Avatar answered Apr 24 '26 20:04

xdazz


Single quotation will of course work, but what if you want to include them in string too and don't want to escape anything inside it? Use custom delimiter like that:

2.0.0p353 :001 > %q['"#{foo}[f]] #or e.g. %q&'"#{foo}[f]& 
 => "'\"\#{foo}[f]"

%q is the equivalent of single quotes and %Q is for double quotes.

like image 43
zrl3dx Avatar answered Apr 24 '26 18:04

zrl3dx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!