[#122369] [Ruby Bug#21392] Data classes do not allow overriding #inspect — "austin (Austin Ziegler) via ruby-core" <ruby-core@...>

Issue #21392 has been reported by austin (Austin Ziegler).

8 messages 2025/06/01

[#122411] [Ruby Bug#21396] Set#initialize should call Set#add on items passed in — "tenderlovemaking (Aaron Patterson) via ruby-core" <ruby-core@...>

Issue #21396 has been reported by tenderlovemaking (Aaron Patterson).

12 messages 2025/06/04

[#122506] [Ruby Feature#21435] Kernel#optional as a conditional #then — "Alexander.Senko (Alexander Senko) via ruby-core" <ruby-core@...>

SXNzdWUgIzIxNDM1IGhhcyBiZWVuIHJlcG9ydGVkIGJ5IEFsZXhhbmRlci5TZW5rbyAoQWxleGFu

11 messages 2025/06/10

[#122557] [Ruby Bug#21445] [BUG] push_mark_stack() called for broken object raised since cd9f447be247478d2eb3da985295735cce20cb23 — "yahonda (Yasuo Honda) via ruby-core" <ruby-core@...>

Issue #21445 has been reported by yahonda (Yasuo Honda).

10 messages 2025/06/19

[#122615] [Ruby Misc#21458] Test 'make install'? — "MSP-Greg (Greg L) via ruby-core" <ruby-core@...>

Issue #21458 has been reported by MSP-Greg (Greg L).

11 messages 2025/06/28

[ruby-core:122428] [Ruby Feature#21219] `Object#inspect` accept a list of instance variables to display

From: "matz (Yukihiro Matsumoto) via ruby-core" <ruby-core@...>
Date: 2025-06-05 06:57:11 UTC
List: ruby-core #122428
Issue #21219 has been updated by matz (Yukihiro Matsumoto).


I prefer the idea of selecting instance variables to output by a hook method. It is tough to choose the name of the method for selection. I suggest `instance_variables_to_inspect` for now. If anyone thinks a more compact name would be better, or that it needs a special naming convention since it is a hook, please make a suggestion.

Matz.


----------------------------------------
Feature #21219: `Object#inspect` accept a list of instance variables to display
https://bugs.ruby-lang.org/issues/21219#change-113608

* Author: byroot (Jean Boussier)
* Status: Open
----------------------------------------
## Context

The default `Object#inspect` implementation is quite useful to have a generic representation of objects in error message and similar places.

However sometimes objects are referencing other objects with a very large `inspect` representation, making error message hard to understand.

In some other cases, some instance variables are holding secrets such as password or private keys, and the default inspect behavior can cause
these secrets to be leaked in logs among other places.

You can of course define your own `inspect` implementation for any object, but it's not as simple as it may seems because you need to handle circular references, otherwise you can end up with a `SystemStackError`.
Also, it's more minor, but since Ruby 2.7, you can no longer access an object's address, so you can't implement an `inspect` method that is consistent with `Object#inspect`

>>From my experience, user defined implementations of `#inspect` are very rare, and I think the above is in part responsible.

## Feature

I think it would be useful if the default `Object#inspect` implementation accepted a list of instance variables to display, so that you could very easily hide internal state, either because it's too verbose, or because it is secret:

```ruby
require 'logger'
logger = Logger.new(STDOUT)

class DatabaseConfig
  def initialize(host, user, password)
    @host = host
    @user = user
    @password = password
  end

  def inspect = super(instance_variables: [:@host, :@user])
end


env = {db_config: DatabaseConfig.new("localhost", "root", "hunter2")}
logger.info("something happened, env: #{env}")
```

```
INFO -- : something happened, env: {db_config: #<DatabaseConfig:0x00000001002b3a08 @host="localhost", @user="root">}
```




-- 
https://bugs.ruby-lang.org/
______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/


In This Thread