timeon
timeon

Reputation: 2194

How do I make a ruby string "\("?

I needed a ruby string with "\(" in it and found the escaping playing trick on me.

"\(" gives me "(" "\\(" gives me "\\("

Upvotes: 1

Views: 62

Answers (1)

Andrew Marshall
Andrew Marshall

Reputation: 96914

"\\(" is correct, the problem is that the result of inspect (which is what IRB uses to display the return value of the last call) is not the same as the actual contents because of the escaping:

puts "\\(".inspect  #prints: "\\("
puts "\\("          #prints: \(

If you don't need interpolation, just use single quotes:

puts '\('  #prints: \(

Upvotes: 7

Related Questions