Why does this Ruby object have both to_s and it checks methods that seem to do the same?

Why to_s this Ruby object use both to_s and inspect methods, which seem to do the same thing?

The p method calls inspect and puts / prints to_s calls to represent the object.

If I run

 class Graph def initialize @nodeArray = Array.new @wireArray = Array.new end def to_s # called with print / puts "Graph : #{@nodeArray.size}" end def inspect # called with p "G" end end if __FILE__ == $0 gr = Graph.new p gr print gr puts gr end 

I get

 G Graph : 0 Graph : 0 
  • Then, why does Ruby have two functions, does the same? What is the difference between to_s and inspect ?
  • And what's the difference between puts , print and p ?

If I comment on the to_s or inspect function, I get the following.

 #<Graph:0x100124b88> #<Graph:0x100124b88> 
+53
object ruby printing
Apr 12 2018-10-12T00:
source share
9 answers

inspect used more for debugging and to_s for end users or for display.

For example, [1,2,3].to_s and [1,2,3].inspect produce a different output.

+44
Apr 12 2018-10-12T00:
source share

inspect is a method that, by default, tells you the class name, the object_id instance, and lists the instance variables.

print and puts are used, as you already know, to set the value of the to_s method of an STDOUT object. As stated in the Ruby documentation, Object#to_s returns a string representing the object used for readability by the end user.

print and puts identical to each other, except puts automatically adds a new line, but print does not.

+29
Apr 12 '10 at 21:45
source share

To compare with Python, to_s is like __str__ , and inspect is like __repr__ . to_s gives a string, while inspect gives a string representation of the object. You can use the latter to build an object if you want.

+13
Apr 12 2018-10-12T00:
source share

Also, on some objects, there is a to_str method that you call when you need a String-like object, not just a string representation. (Try on the IRB: [1,2,3].to_str and it will fail, but [1,2,3].to_s will not.) I feel like I should mention this because I was bitten by it before :)

+5
Apr 13 '10 at 18:39
source share

puts usually prints the result of applying to_s object, and p displays the result of the inspect object.

There is a subtle difference between inspect and to_s :

  • inspect , when applied to an object, returns the hex code of the object along with the instance variable
  • to_s , when applied to an object, returns only the hex code of the object

      class Item
      def initialize (abc)
       @ abc = abc
      end
     end
    
     x = Item.new (22)
    
     puts x #prints object x hex code  
     puts x.inspect #prints object x hex code WITH INSTANCE VARIABLE @abc 
     puts x.to_s #prints object x hex code
    
     px #prints object x hex code WITH INSTANCE VARIABLE @abc
     p x.inspect #prints object x hex code WITH INSTANCE VARIABLE @abc
     p x.to_s #prints object x hex code 
+3
Jul 14 '15 at 12:18
source share
 2.0.0p195 :075 > puts (1..5).to_a # Put an array as a string. 1 2 3 4 5 => nil 2.0.0p195 :076 > puts (1..5).to_a.inspect # Put a literal array. [1, 2, 3, 4, 5] => nil 2.0.0p195 :077 > puts :name, :name.inspect name :name => nil 2.0.0p195 :078 > puts "It worked!", "It worked!".inspect It worked! "It worked!" => nil 2.0.0p195 :079 > p :name # Same as 'puts :name.inspect' :name => :name 

From the Rails Tutorial

+1
Sep 21 '13 at 8:06 on
source share

For those who arrived here after starting Ruby Koans , a simple example of where to_s and inspect differs in output is

 nil.to_s # will yield an empty string, ie "" nil.inspect # will yield the string "nil" 
+1
Feb 11 '16 at 14:47
source share

The answer to this question is Chris Pine Learn To Program book

"The verification method is very similar to to_s, except that the returned string tries to show you the ruby โ€‹โ€‹code to create the object that you passed it to."

Thus, the validation method returns an array, such as ...

 [25, 16, 9, 4, 1, 0] 

Where when puts / to_s will return

 25 16 9 4 1 0 
+1
Jun 02 '17 at 7:58
source share

See the following link for more information and examples explaining the difference between โ€œto_sโ€ and โ€œcheckingโ€, as well as the difference between โ€œputsโ€ and โ€œpโ€. https://rubymonk.com/learning/books/4-ruby-primer-ascent/chapters/45-more-classes/lessons/108-displaying-objects

0
Aug 13 '14 at 19:39
source share



All Articles