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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:27:55+00:00 2026-05-23T22:27:55+00:00

In Ruby, when I do something like this: class Foo … def initialize( var

  • 0

In Ruby, when I do something like this:

class Foo
  ...
  def initialize( var )
    @var = var
  end
  ...
end

Then if I return a foo in console I get this object representation:

#<Foo:0x12345678910234 @var=...........>

Sometimes I have an instance variable that is a long hash or something and it makes reading the rest of the object much more difficult.

My question is: is there a way to set an instance variable in an object to “private” or otherwise invisible so that it won’t be printed as part of the object representation if that object is returned in the console?

Thanks!

  • 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-23T22:27:56+00:00Added an answer on May 23, 2026 at 10:27 pm

    After some quick searching, I don’t think Ruby supports private instance variables. Your best bet is to override your object’s to_s method (or monkey patch Object#to_s) to only output the instance variables you want to see. To make things easier, you could create a blacklist of variables you want to hide:

    class Foo
      BLACK_LIST = [ :@private ]
    
      def initialize(public, private)
        @public = public
        @private = private
      end
    
      def to_s
        public_vars = self.instance_variables.reject { |var|
          BLACK_LIST.include? var
        }.map { |var|
          "#{var}=\"#{instance_variable_get(var)}\""
        }.join(" ")
    
        "<##{self.class}:#{self.object_id.to_s(8)} #{public_vars}>"
      end
    end
    

    Note that they will still be accessible through obj.instance_variables and obj.instance_variable_get, but at the very least they won’t get in the way of your debugging.

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

Sidebar

Related Questions

So I've got a Ruby method like this: def something(variable, &block) .... end And
My ability model looks something like this: class Ability include CanCan::Ability def initialize(user) if
I'd like to do something like this in ruby: safe_variable = begin potentially_nil_variable.foo rescue
In Ruby, is there a way to do something like class Foo attr_reader :var_name
In Ruby, I have this class: class Position attr_reader :x, :y def initialize(x, y)
I tried creating a DB migration in Rails that looked something like this: ruby
We have a model association that looks something like this: class Example < ActiveRecord::Base
I am trying to deserialize an object from XML in Ruby. Something simple like:
I'm trying to create a little Ruby hack to make something like the reverse
In Ruby a class named Foo would be defined with class Foo , used

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.