Ruby methods to display output
The 2 representation methods: inspect and to_s
In Ruby, we can represent or display any object in 2 different ways:
- with the internal representation
- with the string representation
By tacit convention, the internal representation is usually reserved for debugging cases while the string representation is used to display more user-friendly information.
Accordingly, Ruby has 2 specific methods to return the value of each representation.
inspectmethod returns the internal representation
to_smethod returns the string representation
The important thing to remember is that the representation of an object, either the internal one or the string one, is defined by the
to_s and the
inspect methods of the class (or super class) of the object.
The internal representation and the string representation do not have to be different.
For example, they are the same for arrays.
In fact, in Ruby's documentation,
Array#inspect are aliases: thet are exactly the same method.
This is also the case for hashes or integers.
The 3 display methods: print, puts and p
There are 3 methods intended to display data in a Ruby REPL:
Kernel#puts(puts is for "put string")
These 3 display methods have different formats of output and different return values.
In the previous table, there is a surprising case:
Kernel#puts with an array passed in as an argument. Just be aware that this specific case exists.
Now, let's play around and have a look at some examples to see the differences.
print, puts and the new line
text_1 = 'Sun is shining' text_2 = 'Winter is coming' text_3 = 'Where is the rain?' print text_1 print text_2 print text_3
The execution of the previous code outputs:
Sun is shiningWinter is comingWhere is the rain? => nil
As we can see,
Kernel#print doesn't automatically add a new line.
We can change that by using
text_1 = 'Sun is shining' text_2 = 'Winter is coming' text_3 = 'Where is the rain?' puts text_1 puts text_2 puts text_3
The output now shows a string per line:
Sun is shining Winter is coming Where is the rain? => nil
print, puts and p with arrays
If we execute the following code:
array = [1, nil, nil, 2] p array puts '--' puts array puts '--' print array
The output is the following:
[1, nil, nil, 2] -- 1 2 -- [1, nil, nil, 2]
We can see that when we call
Kernel#p (at the top of the code) and
Kernel#print (at the bottom of the code) with an array passed in as an argument, an array is output.
However, when we call
Kernel#puts (in the middle of the code), each element of the array is output on a new line.
Moreover, we have 2 empty lines between the output of number 1 and the output of number 2.
This shows that
Kernel#puts does output the string representation of each element of the array.
The string representation of
nil is an empty line.
Since elements at index 1 and 2 of the array are
nil, 2 empty lines are displayed.
Now, let's change the previous code like this:
array = [1, nil, nil, 2] p array puts '--' puts array.to_s puts '--' print array
The output is the following:
[1, nil, nil, 2] -- [1, nil, nil, 2] -- [1, nil, nil, 2] => nil
Wow! Now we have 3 magnificent arrays. How come?
When we call
Array#to_s on the
array local variable, it returns the string representation of an array, which is actually a string:
"[1, nil, nil, 2]".
Then we pass this string to the method
Since the argument is already a string, and since the method
String#to_s returns the same string that is passed in as an argument to it, the array is output as it is.
Array#inspect are exactly the same methods (they are aliases), we could have achieved the same result with the following code:
array = [1, nil, nil, 2] p array puts '--' puts array.inspect puts '--' print array
Author: Dimitri Alamkan
Initial publication date: