inspect method of ruby

inspect is a method that will return a string with literal representation of the object it is called on.

 

puts (1..5).to_a.inspect

=> [1, 2, 3, 4, 5]

 

puts :name.inspect

=> :name

 

by the way, using inspect to print an object is so common that there is a shortcut for it, the p function:

 

p :name    #this will first exec :name.inspect, then puts the returning string.

 

this is equivalent with:

 

puts :name.inspect 

你可能感兴趣的:(Ruby,inspect)