In this section, we will learn what the format sequence is and how to use it in Ruby.
What is Format Sequences in Ruby?
Format Sequence (AKA format specifiers) in Ruby is a way of formatting and modifying the way a value is printed on the output stream.
For example, if you divide a value by another one and the result is a floating value with a lengthy decimal numbers, your program will print the entire decimal part as well which we might not need.
So here the format sequence helps you to style the floating number and define how many numbers of the fraction part you want it to be displayed.
Other than modifying a float number, using format sequence, we can mix in other values in a string and create a styled string value.
Alright, enough with theory and let’s jump into the format sequences and see how they actually work.
List of Format Sequences in Ruby:
Three of the most used format sequences in Ruby are:
%s: this is used whenever we want to add a string value into another string. For example, you have a variable that contains a string value and now want to put that string into a section of another string! So what you need to do is to use this format sequence (%s) in the place where you want the value of the target variable to appear.
%i: this is used whenever we want to add an integer value into a string value.
%f: this is used whenever we want to add a float number into a string value.
Ruby format Method
In Ruby, in order to use format sequences, we use the `format` method. It takes a string value that contains format sequences and other types of values that we want to pass to the string value to replace the format specifiers in that string.
Ruby format Method Syntax:
format (“string value with format specifiers”, value1, value2, value_n)
The first argument, as mentioned before, is a string value that contains the format specifiers.
The rest of arguments are the values we want to replace the format specifiers in the first argument with these values.
Note that the number of format specifiers in the first argument should match the number of arguments we pass to the `format` method. For example, if there are two %f format specifier in the string value, then we should have two other arguments set in the `format` method when it is called so that they become the replacement of the specifiers.
Note that the `format` method returns a new string as a result of calling it.
Example: using format Sequences in Ruby
fValue = 4.33 fValue2 = fValue / 3.22 result = format("The result of the division is: %f. Also the value of the fValue is: %f", fValue2, fValue) puts result
Output:
The result of the division is: 1.344720. Also the value of the fValue is: 4.330000
How does format sequence work in Ruby?
In the example above, the `format` method contains two %f format specifiers.
This means we need to pass two floating values after the first argument of the method as well in order to replace those specifiers!
Note that if you don’t provide the two specifiers in this example (for example, set one or 3 values which is more than available specifiers in the first argument) you’ll get an error instead.
Ruby Format Sequence Width
Alright, if you take a look at the example above, you can see there are too many floating numbers in the result of calling the format method and setting the floating values in there!
But in our program, we may not need a floating number with this amount of details on the decimal part! For example, a floating number with just 2 numbers on the decimal part might do the job perfectly fine!
So for this reason, format sequences come with modifiable width!
This means you can change the length of a value you put as the replacement of a format specifier!
Let’s get into the syntax of using width with format specifiers and from there, we will continue our discussions.
Ruby Format Sequence Width Syntax:
%number format-specifier
As you can see, between the % symbol and the format-specifier, we set the width of the value we want to replace the target specifier with.
For example:
%10i: this means the length of the integer value that we set as the replacement of the specifier should at least be 10 characters (number) long. If the integer value is less than that, then the left side of the integer value will be padded with white space until the width of the value reaches to 10 characters long.
%4s: this also means the length of the target string value should at least be 4 characters long! Now just like the integer specifier, if the length of the target string value is less than 4 characters, then the left side of the value will be padded with white space until the minimum width of the value reaches to 4 characters long.
Note that defining width for a floating number (%f specifier) is a bit different from what we’ve seen for the integer and string specifiers.
For the %f specifier we can set two numbers:
%number1.number2 f
The `number1` defines the minimum width the target floating point can have (just like the way we said for the string and integer values).
But the `number2` which is set on the right side of the decimal point, defines the MAXIMUM width of the fraction part of a float number!
For example, if the `number2` is set to 3, that means the width of the fraction part of the float number cannot be greater than 3 characters (three numbers).
Example: using format sequence width in Ruby
Aright, let’s refactor the example above and set the maximum width of the fraction part of the floating numbers to 2 this time.
fValue = 4.33 fValue2 = fValue / 3.22 result = format("The result of the division is: %.2f. Also the value of the fValue is: %.2f", fValue2, fValue ) puts result
Output:
The result of the division is: 1.34. Also the value of the fValue is: 4.33
As you can see, this time, we got two digits on the fraction part for the floating values
Note: the value we set for the minimum width of a value is optional and can be ignored. For this reason, we didn’t set any value for the minimum width in the example above.