String Delimiters %q in Ruby

MD Aminul Islam Feb 02, 2024
  1. Use the %Q in Ruby
  2. Use the %q in Ruby
  3. Use the % in Ruby
String Delimiters %q in Ruby

Sometimes you may need to print an equation, but when you try it, the program automatically solves the equation and prints the resulting output. But there are some techniques through which you can do this.

This article will discuss string delimiters in Ruby, and also we are going to look at some examples to make the topic easier. We will see the use of %q, %Q, and %.

Use the %Q in Ruby

In our below example, we will illustrate the use of %Q. Let’s look at the below example.

puts %(3 + 2 =\n#{3 + 2})

The above single line of the program will provide you with the below-resulting output, and if you look at the output, you will see that the mathematical equation is solved before printing.

3 + 2 =
5

Use the %q in Ruby

In our below example, we will demonstrate the use of %q. Let’s look at the below example.

puts %q(3 + 2 =\n#{ 3 + 2 })

The above single line of the program will provide you with the below-resulting output. You will see that the mathematical equation remains unsolved before printing.

3 + 2 =\n#{ 3 + 2 }

Use the % in Ruby

In our example below, we will demonstrate the use of %. Let’s look at the below example.

puts %(3 + 2 =\n#{3 + 2})

The above single line of the program will provide you with the below-resulting output. You will see that the mathematical equation is solved before printing. It is mostly similar to the %Q.

3 + 2 =
5

Remember %Q is the default of % notation.
Please note that all the code this article shares is written in Ruby.

MD Aminul Islam avatar MD Aminul Islam avatar

Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.

LinkedIn