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 7571779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:45:15+00:00 2026-05-30T15:45:15+00:00

i override the to_s method to get pretty output when i use puts but

  • 0

i override the to_s method to get pretty output when i use puts but at the same time i seem to loose my ability to inspect the object. Is there a way to get the normal output of inspect while overriding to_s ?

class Person
  attr_accessor :first, :last, :birthdate
  def initialize(first=nil, last=nil, birthdate=nil)
    @first, @last, @birthdate = first, last, birthdate
  end
  def age
    if birthdate
      Time.now.year-birthdate
    else
      0
    end
  end
  def to_s
    "#{@first} #{@last} (#{age})"
  end
end

me = Person.new("Peter", "Marien", 1962)
p me  >>Peter Marien (50)
p me.inspect  >>"Peter Marien (50)"

#Need #<Person:0x1ec2550 @first="Peter", @last="Marien", @birthdate=1962>
  • 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-30T15:45:17+00:00Added an answer on May 30, 2026 at 3:45 pm

    Ruby documentation clearly states, that by default inspect uses to_s as its output.

    If you don’t need the address of the object, you could provide your own inspect, to have almost the same behavior:

    class Person
      def inspect
        vars = self.instance_variables.
          map{|v| "#{v}=#{instance_variable_get(v).inspect}"}.join(", ")
        "<#{self.class}: #{vars}>"
      end
    end
    

    But you could also install a gem called awesome_print that will give very nice output.
    First in console:

    $ gem install awesome_print
    

    then in irb or your script:

    require 'awesome_print'
    ap Person.new("John")
    

    There is also a built-in pp library, which have similar purpose. Still it is not immune (at least in Ruby 1.9.2-p290) to overriding of to_s.

    A quick example of pp:

    require 'pp'
    pp Person.new("John")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If subclass in objective-c wants to override a super class's method, does it have
I am trying to override the DataGridViewTextBoxCell's paint method in a derived class so
It doesn't seem to possible to override a plugin execution's goal definition. Let say
I've overrode the method find for ActiveRecord::Base and that went pretty well, now I
How can I get the value of userId passed to this method in my
I know this should be fairly simple but I don't get it anyhow. I've
My question, how to override model manager's method for passing default parameter, if it
I have created an FxCop rule that checks for DateTime.Now use. It works pretty
I have a User model witch i have override the to_param method. i made
I just came across something pretty weird to me : when you use the

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.