Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 719701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:40:24+00:00 2026-05-14T05:40:24+00:00

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

  • 0

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. – Ruby's 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:40:25+00:00Added an answer on May 14, 2026 at 5:40 am
      obj.inspect => string
    

    Returns a string containing a human-readable representation of obj. If not overridden, uses the to_s method to generate the string.

       [ 1, 2, 3..4, 'five' ].inspect   #=> "[1, 2, 3..4, \"five\"]"
       Time.new.inspect                 #=> "Wed Apr 09 08:54:39 CDT 2003"
    

     obj.to_s => string
    

    Returns a string representing obj. The default to_s prints the object‘s class and an encoding of the object id. As a special case, the top-level object that is the initial execution context of Ruby programs returns “main.’‘

    source

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Python __str__ / __repr__ are called by default by print function. In Java
I call a __repr__() function on object x as follows: val = x.__repr__() and
It seems there are different ways the __repr__ function can return. I have a
What would be the best way to handle the __repr__() function for an object
What is the difference between __str__ and __repr__ in Python?
How do I make something like user-defined __repr__ in Python? Let's say I have
An ordinary object, I can use o.__repr__() to see something like '<__main__.A object at
In Python 2.x, using backticks to get decimal string from int object is Horrible
I've successfully extended python with C, thanks to this handy skeleton module . But
I'm attempting to use the __rsub__ function in a class I've made called Fraction.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.