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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:01:54+00:00 2026-05-17T19:01:54+00:00

Short version: The default inspect method for a class displays the object’s address.* How

  • 0

Short version: The default inspect method for a class displays the object’s address.* How can I do this in a custom inspect method of my own?

*(To be clear, I want the 8-digit hex number you would normally get from inspect. I don’t care about the actual memory address. I’m just calling it a memory address because it looks like one. I know Ruby is memory-safe.)

Long version: I have two classes, Thing and ThingList. ThingList is a subclass of Array specifically designed to hold Things. Due to the nature of Things and the way they are used in my program, Things have an instance variable @container that points back to the ThingList that holds the Thing.

It is possible for two Things to have exactly the same data. Therefore, when I’m debugging the application, the only way I can reliably differentiate between two Things is to use inspect, which displays their address. When I inspect a Thing, however, I get pages upon pages of output because inspect will recursively inspect @container, causing every Thing in the list to be inspected as well!

All I need is the first part of that output. How can I write a custom inspect method on Thing that will just display this?

#<Thing:0xb7727704>

EDIT: I just realized that the default to_s does exactly this. I didn’t notice this earlier because I have a custom to_s that provides human-readable details about the object.

Assume that I cannot use to_s, and that I must write a custom inspect.

  • 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-17T19:01:55+00:00Added an answer on May 17, 2026 at 7:01 pm

    You can get the address using object_id and multiplying it by 2* and display it in hex using sprintf (aka %):

    "#<Thing:0x%08x>" % (object_id * 2)
    

    Of course, as long as you only need the number to be unique and don’t care that it’s the actual address, you can just leave out the * 2.

    * For reasons that you don’t need to understand (meaning: I don’t understand them), object_id returns half the object’s memory address, so you need to multiply by 2 to get the actual address.

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

Sidebar

Related Questions

Short version: Can we read from dozens or hundreds of table partitions in a
Short version: I want to trigger the Form_Load() event without making the form visible.
Short version: I'm wondering if it's possible, and how best, to utilise CPU specific
Short version: assuming I don't want to keep the data for long, how do
Short Version: When I've created a Channel using ChannelFactory on a client which uses
I am writing an application to manage user access to files. The short version
Short of copying the entire .netbeans directory is there any way to transfer custom
Short of inserting a try/catch block in each worker thread method, is there a
In every Visual Studio.NET version you can set keyboard shortcuts using menu Tools ->
Short version: I want a way to run somefunction(username) and have it return 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.