tags:

views:

114

answers:

2

The __repr__ function of python is fancy as it is called when print OBJECT is used automatically.

Is there a ruby equivalence for it? I thought it was to_s, but, I had p OBJECT doesn't seem to call the to_s method.

=== Added ===

I got something wrong, p OBJECT seems to call to_s method as follows. I got some hints from my the answers to my other question. - http://stackoverflow.com/questions/2625093/rubys-to-s-method-question-from-axe-book-2nd-edition

# Sample code from Programing Ruby, page 24
class Song
  def to_s
    "Song"
  end
end

class Songson < Song
  def to_s
    super + "<Songson>"
  end
end

song = Songson.new()
p song
+2  A: 

p object uses #inspect.

steenslag
+3  A: 
TheMachineCharmer
@TheMachineCharmer : Could you check my other question - http://stackoverflow.com/questions/2625667/why-ruby-has-to-s-and-inspect ? <<If not overridden, uses the to_s method to generate the string.>> <-- doesn't seem to wrok.
prosseek
Inspect - If not overridden, uses the to_s method to generate the string.---Completely true.@prosseek - Try writing a simple class and do to_s and inspect on its instance - both will return the same.inspect has been overridden on predefined classes like Array,Hash ... that's why they print differently.
Webbisshh