The Wayback Machine - https://web.archive.org/web/20240917123658/https://www.geeksforgeeks.org/ruby-string-interpolation/
Open In App

Ruby | String Interpolation

Last Updated : 02 Jan, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

String Interpolation, it is all about combining strings together, but not by using the + operator. String Interpolation works only when we use double quotes (“”) for the string formation. String Interpolation provides an easy way to process String literals. String Interpolation refers to substitution of defined variables or expressions in a given String with respected values. This is how, string Interpolation works, it executes whatever that is executable. Let’s see how to execute numbers and strings. Syntax:

#{variable}

The above syntax specifies that the things inside {} are executable objects or variables. Below are the examples to understand : Example : 

Ruby




# Ruby Program of String Interpolation
a = 1
b = 4
puts "The number #{a} is less than #{b}"


Output :

The number 1 is less than 4

Example: 

Ruby




# Ruby Program of String Interpolation
 
s = 'Groot';
n = 16
 
# takes this as entire new string
puts "s age = n";
 
# interpolation
puts "#{s} age=#{n}";
 
# if number not converted to string throws an error
puts s+" age="+n.to_s;


Output:

s age=n
Groot age=16
Groot age=16

So, the only advantage, We don’t have to convert numbers into a string because ruby takes care of it. let’s say string=”weds” In string concatenation, if we are concatenating strings like (“hela “+string+” puri”), it creates 3 string objects, it moves from right-to-left, it first creates ” puri” object1, + method sticks the puri with the existing string together and returns weds puri object2 and this again gets stick by + method and forms the final hela weds puri object3. Eventually, the objects(1, 2) created in this process gets terminated as they are of no use. In String Interpolation, it creates an only one object for (“hela “+string+” puri”) and embeds the existing string(weds)to it. In both the case, in the end, it creates one object but why prefer Interpolation over concatenation. Two reasons, no need for additional conversions of datatypes to string ruby takes care of it and this process is most of the developers kinda use.

Escape sequences

Strings can not only contain text. They can also contain control characters. Difference between single or double quotes is that double quotes allow for escape sequences while single quotes do not allow for it. Example : 

Ruby




# Ruby program of string Interpolation
puts 'guardians\nGroot';
 
# gets executes and prints Groot on a newline.
puts "guardians\nGroot";
 
# takes care of control characters.
puts "hela\nweds\tpuri";


Output:

guardians\nGroot
guardians
Groot
hela
weds    puri

In above example, \n is the escape sequence that stands for the “newline” character.



Previous Article
Next Article

Similar Reads

Difference between Ruby and Ruby on Rails
Ruby is an object-oriented scripting language known for its simplicity and security, commonly used in developing desktop applications. Ruby on Rails is a web application framework built on Ruby, designed for creating database-driven web apps using the MVC architecture. It emphasizes principles like "Don’t Repeat Yourself" (DRY) and is known for its
4 min read
Ruby | String inspect Method
inspect is a String class method in Ruby which is used to return a printable version of the given string, surrounded by quote marks, with special characters escaped. Syntax: str.inspect Parameters: Returns: Here, str is the given string. Example 1: # Ruby program to demonstrate # the inspect method # Taking a string and # using the method puts
1 min read
Ruby | String hex Method
hex is a String class method in Ruby which is used to treats the leading characters from the given string as a string of hexadecimal digits (with an optional sign and an optional 0x) and returns the corresponding number. Zero is returned on error. Syntax: str.hex Parameters: Here, str is the given string. Returns: A corresponding number. Example 1:
1 min read
Ruby | String include? Method
include? is a String class method in Ruby which is used to return true if the given string contains the given string or character. Syntax: str.include? Parameters: Here, str is the given string. Returns: true if the given string contains the given string or character otherwise false. Example 1: # Ruby program to demonstrate # the include? method #
1 min read
Ruby | String delete_suffix! Method
delete_suffix! is a String class method in Ruby which is used to return a copy of the given string with trailing suffix deleted or nil if no change was made. Syntax:str.delete_suffix! Parameters: Here, str is the given string. Returns: A copy of the given string with trailing suffix deleted or nil if no change was made. Example 1: C/C++ Code # Ruby
1 min read
Ruby | String dump Method
dump is a String class method in Ruby which is used to generate a version of the given string with all non-printing characters replaced by \nnn notation and all special characters escaped. Syntax: str.dump Parameters: Here, str is the given string. Returns: A new string with all non-printing characters replaced by \nnn notation and all special char
1 min read
Ruby | String each_byte Method
each_byte is a String class method in Ruby which is used to passes each byte in the given string to the given block or returns an enumerator if no block is given. Syntax: str.each_byte {|integer| block } Parameters: Here, str is the given string. Returns: An enumerator. Example 1: # Ruby program to demonstrate # the each_byte method # Taking a stri
1 min read
Ruby | String Basics
In Ruby, string is a sequence of one or more characters. It may consist of numbers, letters, or symbols. Here strings are the objects, and apart from other languages, strings are mutable, i.e. strings can be changed in place instead of creating new strings. String's object holds and manipulates an arbitrary sequence of the bytes that commonly repre
5 min read
Ruby | String * Method
String# * is a String class method in Ruby which is used to returns a new String containing integer copies of the receiver. Here, integer must be greater than or equal to 0. Syntax: str * Integer Parameters: Here, str is the required string and integer is the number of copies. Returns: This method returns the String containing integer copies of the
1 min read
Ruby | String + Method
+() is a String class method in Ruby which is used to return a new string that contains the other specified string concatenated to the given string. Syntax: str + other_str Parameters: Here, str and other_str are the strings which are to be concatenated. Returns: Returns the concatenated string. Example 1: #ruby 2.3.1 # Ruby program to demonstrate
1 min read
Ruby | String << Method
<<() is a String class method in Ruby which is used to append the given object to the string. If the object is an integer, then by default it is considered a codepoint and converted to a character before being appended. Syntax: str << "object" Parameters: Here, str is the specified string and object is appended to the string.
1 min read
Ruby | String insert Method
insert is a String class method in Ruby which is used to inserts the specified string before the character at the given index, modifying the given one. Negative indices count from the end of the string, and insert after the given character. Syntax: str.insert(index, other_str) Parameters: Here, str is the given string. Returns: A modified string. E
1 min read
Ruby | String chomp! Method
chomp! is a String class method in Ruby which is used to returns new String with the given record separator removed from the end of str (if present). chomp method will also removes carriage return characters (that is it will remove \n, \r, and \r\n) if $/ has not been changed from the default Ruby record separator, t. If $/ is an empty string, it w
1 min read
Ruby | String chop! Method
chop! is a String class method in Ruby which is used to return a new String with the last character removed. Both characters are removed if the string ends with \r\n, b. It will return nil if the string is empty. Syntax:str.chop!Parameters: Here, str is the given string.Returns: A new string having no record separator or nil if the string is empty.
1 min read
Ruby | String chop Method
chop is a String class method in Ruby which is used to return a new String with the last character removed. Both characters are removed if the string ends with \r\n, b. Applying chop to an empty string returns an empty string. Syntax:str.chopParameters: Here, str is the given string.Returns: A new string having no record separator. Example 1: C/C++
1 min read
Ruby | String capitalize() Method
capitalize is a String class method in Ruby which is used to return a copy of the given string by converting its first character uppercase and the remaining to lowercase. Syntax: str.capitalize Parameters: Here, str is the given string which is to be converted. Returns: Copy of the string with the first character in uppercase and remaining in lower
1 min read
Ruby | String capitalize! Method
capitalize! is a String class method in Ruby which is used to return a copy of the given string by converting its first character uppercase and the remaining to lowercase. The only difference between capitalize and capitalize! method is that capitalize! method will return nil if no changes are made. Syntax:str.capitalize! Parameters: Here, str is t
1 min read
Ruby | String casecmp Method
casecmp is a String class method in Ruby which is Case-insensitive version of String#<=>. For now, case-insensitivity only works on characters A-Z/a-z, not all of the Unicode characters. This method is different from casecmp! method. Syntax: str.casecmp(other_str) Parameters: Here, str is the given string to be checked and other_str is the st
1 min read
Ruby | String casecmp? Method
casecmp? is a String class method in Ruby which is used to return true if both the string are equal after Unicode case folding and false if they are not equal. Syntax: str.casecmp?(other_str) Parameters: Here, str is the given string to be checked and other_str is the string to which str is compared. Returns: This method will returns the numbers ba
1 min read
Ruby | String length Method
length is a String class method in Ruby which is used to find the character length of the given string. Syntax: str.length Parameters: Here, str is the string whose length is to be calculated Returns:It will return the character length of the str. Example 1: # Ruby program to demonstrate # the length method # Taking a string and # using the method
1 min read
Ruby | String reverse Method
reverse is a String class method in Ruby which is used to return a new string with the characters from the given string in reverse order. Syntax: str.reverse Parameters: Here, str is the string which is to be reversed. Returns: This method returns a new string in reversed order. Example 1: # Ruby program to demonstrate # the reverse method # Taking
1 min read
Ruby | String empty? Method
empty? is a String class method in Ruby which is used to check whether the string length is zero or not. Syntax: str.empty? Parameters: Here, str is the given string which is to be checked. Returns: It returns true if str has a length of zero, otherwise false. Example 1: # Ruby program to demonstrate # the empty? method # Taking a string and # usin
1 min read
Ruby | String concat Method
concat is a String class method in Ruby which is used to Concatenates two objects of String. If the given object is an Integer, then it is considered a codepoint and converted to a character before concatenation. Syntax:String_Object.concat(String_Object) Parameters: This method can take the string object and normal string as the parameters. If it
1 min read
Ruby | String bytesize method
bytesize is a String class method in Ruby which is used to get the length of the given string in bytes. Syntax: str.bytesize Parameters: Here, str is the given string. Returns: This method returns the length of the str in bytes. Example 1: # Ruby program to demonstrate # the bytesize method # Taking a string and # using the method puts "Geeksf
1 min read
Ruby | String bytes Method
bytes is a String class method in Ruby which is used to return an array of the bytes for the given string. Syntax: str.bytes Parameters: Here, str is the specified string. Returns: An array of bytes. Example 1: # Ruby program to demonstrate # the bytes method # Taking a string and # using the method puts "String".bytes Output: 83 116 114
1 min read
Ruby | String byteslice Method
byteslice is a String class method in Ruby which is used for the byte reference. Syntax: str.byteslice Parameters: Here, str is the specified string. Returns: A substring of one byte at that position if only a single integer passed. A substring starting at the offset given by the first, and a length is given by the second if the two integers passed
1 min read
Ruby | String <=> Method
<=>() is a String class method in Ruby which is used for comparisons of the strings. This method returns -1, 0, +1, or nil depending on whether the specified string is less than, equal to, or greater than other_string. Syntax: str other_string Parameters: Here, str and other_string are the strings which are compared. Returns: Returns nil if t
1 min read
Ruby | String == method
==() is a String class method in Ruby which is used to check whether str==obj or not. Syntax: str == obj Parameters: Here, str is the given string and obj is the object to be compared. Returns: True or False based on the equality of the two strings. Example 1: #ruby 2.3.1 # Ruby program to demonstrate # the == method # Taking a string and # using t
1 min read
Ruby | String === Method
===() is a String class method in Ruby which is used to check whether str==obj or not. This method is similar to == method of string class. Syntax: str === obj Parameters: Here, str is the given string and obj is the object to be compared. Returns: True or False based on the equality of the two strings. Example 1: #ruby 2.3.1 # Ruby program to demo
1 min read
Ruby | String center() method
center is a String class method in Ruby which is used to centers the given string in width. If the specified width is greater than the length of the given string, then this method will return a new string of the specified width with the given string centered and padded otherwise it returns only given string. Syntax: str.center(width, padstr='')-
1 min read