Table of contents
1.
Introduction 
2.
String Interpolation
3.
String Interpolation Using the sprintf Method
4.
Frequently Asked Questions
4.1.
What are Objects in Ruby?
4.2.
What is a String in Ruby?
4.3.
What is Multibyte Character in Ruby?
4.4.
What is string interpolation in Ruby?
4.5.
What are Global Functions in Ruby?
5.
Conclusion
Last Updated: Mar 27, 2024

String Interpolation with sprintf in Ruby

Author Rajat Agrawal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

Ruby is a general-purpose, dynamic, reflective, object-oriented programming language. Everything in Ruby is an object. Ruby's development aimed to create a user interface between human programmers and the underlying computational machinery.

The string data type is the most common data type found in all the programming languages.

A string is a collection of one or more characters, which might be letters, numbers, or symbols. In contrast to other programming languages, Ruby's strings are objects that may be modified in-place rather than replaced entirely.

Let’s learn about String Interpolation in Ruby.

String Interpolation

String interpolation is the process of replacing specified variables or expressions in a given String with acceptable values. It is all about concatenating strings together but not using the + operator.

String interpolation only functions when the string creation is done using double quotes (" "). String literals may be processed simply using String Interpolation.

Syntax:

#{variable} 
You can also try this code with Online Ruby Compiler
Run Code


The above syntax indicates that the elements inside the curly braces { }  are variables or executable objects.

Let’s understand this with the help of an example.

Example1:

name = "Ninjas"
designation1 = "Developers"
designation2 = "Data Scientists"

#string interpolation
puts "#{name} are #{designation1} and #{designation2}."
You can also try this code with Online Ruby Compiler
Run Code


Output:

Ninjas are Developers and Data Scientists.
You can also try this code with Online Ruby Compiler
Run Code

In the above example, we see that the variables name, desigantion1, and designation2 are easily interpolated in the string using string interpolation.

Example2:

var1 = 10
var2 = 20

#string interpolation
puts "var1 is greater than var2: #{var1>var2}."
puts "var1 is less than var2: #{var1<var2}."
You can also try this code with Online Ruby Compiler
Run Code


Output:

var1 is greater than var2: false.
var1 is less than var2: true.
You can also try this code with Online Ruby Compiler
Run Code

In the above example, we see that we can also perform any type of operations on variables inside the curly braces { }. Since the var1 is less than var2, it gives false in the case of var1>var2 and true in the case of var1<var2.

String Interpolation Using the sprintf Method

In Ruby, we apply the sprintf method to ease the creation of formatted string data.

We use the percentage sign (" % ") to specify a format code within a string. Additionally, we use the "," or "%" symbol after the string to indicate the variables.

Let’s understand this with the help of an example.

Example1:

# using sprintf method
res1 = sprintf("PI is about %.4f", Math::PI)
res2 = sprintf("Coding %s", "Ninjas")
puts res1
puts res2
You can also try this code with Online Ruby Compiler
Run Code


Output:

PI is about 3.1416
Coding Ninjas
You can also try this code with Online Ruby Compiler
Run Code

In the above example, we can see that we can interpolate the variables into a string using the sprintf function.

The advantage of this type of interpolation is that the format string may provide parameters, such as the number of decimal places to show in a Float

There is even an operator form of the sprintf function. To use it, just place a percent operator between a format string and the parameters to be interpolated into it.

Example2:

# using % operator method
res1 = "PI is about %.4f" % Math::PI
res2 = "Coding %s" % "Ninjas"
puts res1
puts res2
You can also try this code with Online Ruby Compiler
Run Code


Output:

PI is about 3.1416
Coding Ninjas
You can also try this code with Online Ruby Compiler
Run Code

In the above example, we can see that we don't even have to write the sprintf if we are using the operator.

Frequently Asked Questions

What are Objects in Ruby?

In Ruby, everything is an object. All objects have a unique identification; they can also maintain a state and exhibit behaviour in response to messages. Usually, these messages are sent out via method calls. A string is an example of a Ruby object.

What is a String in Ruby?

A string is a collection of one or more characters, which might be letters, numbers, or symbols. Ruby's strings are objects that may be modified in-place rather than replaced entirely.

What is Multibyte Character in Ruby?

A character made up of sequences of one or more bytes is known as a Multibyte Character. Each byte sequence represents the extended character set as a single character.

What is string interpolation in Ruby?

String interpolation is the process of replacing specified variables or expressions in a given String with acceptable values. It is all about concatenating strings together but not using the + operator.

What are Global Functions in Ruby?

Kernel-specified methods and any methods defined at the top-level, outside of any classes, are Global Functions. Global functions are defined as private methods of the Object class.

Conclusion

In this article, we have extensively discussed String Interpolation using sprintf in Ruby with the help of code examples.

If you want to learn more, check out our articles on Object Marshalling in RubyTainting Objects in RubyCopying Objects In RubyHow To Invoke Global Functions In Ruby?, and Object References in Ruby.
Check out this problem - Redundant Braces

Do upvote our blog to help other ninjas grow.

Happy Coding!

Live masterclass