ruby sprintfRuby was created in 1993 by Matsumoto.  The syntax of Ruby is simple and easy. It is one of the reasons for its tremendous popularity. A new developer will definitely find it easy to learn this important and useful programming language. This language is object-oriented and is also used for server side scripting. Ruby has features which are like that of Smalltalk, Perl and Python.  It is open source, but subject to a license.

Ruby IO forms an important part of the Ruby programming language. It provides a rich set of functions which enables Ruby programmers to create useful and elegant programs. The sprintf function is an important IO function which is widely used in Ruby programs. Today we walk you through the sprintf function. We assume that you are aware about the basics of Ruby programming. Even if you do not have prior programming experience, this special course on Ruby can help get you up to speed in no time (well, almost!). If you just need a quick brush up on Ruby, you can read through our step by step guide to learning Ruby.

What is Sprintf?

You may be familiar with the Ruby printf function (you can read more about Ruby print in this deep dive tutorial). Conceptually, sprintf is pretty similar to printf. The major difference is that instead of printing to the screen, sprintf saves the formatted text into a string variable. The syntax of format function is as follows

sprintf( format sequence[, argument])

This returns a string in which the argument is formatted according to a format  sequence. The syntax of a format sequence is follows

%[flags][width][.precision]type

A format sequence has a “%” sign which is followed by optional flags, width and precision indicators. The sequence is terminated with a field type character.

The table here displays the field type characters:

Field Description
b The argument is converted to a binary number.
B Similar to ‘b.’ However, uses an uppercase OB for prefix.
 d Argument is converted into a decimal number.
i same as ‘d’
o Argument is converted into an octal number.
u same as ‘d’
x Argument is converted into a hexadecimal number. The negative numbers are displayed with prefix f

 

Have a look at the following table. Here the field is of type Float:

Field Description
e converts the floating point argument to exponential format.
E Same as ‘e.’ However, uses the uppercase E to for exponent
f Converts floating point argument to decimal format along with the number of digits after the decimal point.
g It converts a floating point number which uses exponential form
G Same as ‘g.’ However it uses an uppercase ‘E’ in exponent format.
a Converts a floating point argument with  fraction part as a hexadecimal and exponential part as decimal.
A Same as “a.” However uses uppercase ‘X’ and ‘P’.

 

The other formats are covered in the following table:

Field Description
c Argument is either a number code for an individual character or an individual character string itself. 
p evaluates argument.inspect
s Its argument is a string to be substituted.
% Does not accept any argument. The % sign will itself be displayed.

To learn more about Ruby print and sprintf formats, you can look up this practical training course.

Examples of the Ruby Sprintf Function

To return integer values –  %d

To return octal values – % o

To return hexadecimal values- %x and %X

To learn more about how to print and format different strings in Ruby, check out this course.

To return binary values- %b and % B are used

To return exponential –  %e is used for exponential format

% f and %g is used to print floating point values.

The field width is optional. The field may be followed by a period and a precision. It specifies the minimum number of characters which will come in the spaces.

The sprintf  function is a versatile and useful function which helps the programmer to display the output appropriately. Go through the examples above to get exposure to the power and range of this function. We strongly encourage you to experiment and try these code snippets on your own. That’s the best way to learn. You can also take this special course designed to help you learn Ruby from scratch. It’s a good way to start and find your element. Once you’re more comfortable and read to move to the next level, you can take this advanced Ruby course to gain mastery!

Ruby students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy for Business.

Request a demo