How to use Alternate Quoting Syntax In Ruby
Other alternate quoting syntaxes supported by Ruby include %q{} for single quotes and %Q{} for double ones. In this case, we don’t need escaping quotations inside brackets.
Example:
string_with_single_quote = %q{She said 'Hello'}
string_with_double_quote = %Q{He said "Hi"}
puts string_with_single_quote
puts string_with_double_quote
Output
She said 'Hello' He said "Hi"
How to Escape single and double quotes in a string in Ruby?
To handle strings containing single or double quotes in Ruby is not simple since putting these characters themselves within the string may also present difficulties. Your codes can give a syntax error or work in a way unintended just because of unescaped quotes. Luckily, Ruby has different ways to escape these symbols inside the text. This article will delve into three common methods to Escape single and double quotes in a string.
Table of Content
- Using Backslashes (\):
- Using Alternate Quoting Syntax:
- Using Here Documents:
Contact Us