* [ruby-core:121551] [Ruby Feature#21219] `Object#inspect` accept a list of instance variables to display
@ 2025-04-07 13:29 byroot (Jean Boussier) via ruby-core
2025-04-07 17:37 ` [ruby-core:121558] " mame (Yusuke Endoh) via ruby-core
` (8 more replies)
0 siblings, 9 replies; 10+ messages in thread
From: byroot (Jean Boussier) via ruby-core @ 2025-04-07 13:29 UTC (permalink / raw)
To: ruby-core; +Cc: byroot (Jean Boussier)
Issue #21219 has been reported by byroot (Jean Boussier).
----------------------------------------
Feature #21219: `Object#inspect` accept a list of instance variables to display
https://bugs.ruby-lang.org/issues/21219
* 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/
^ permalink raw reply [flat|nested] 10+ messages in thread
* [ruby-core:121558] [Ruby Feature#21219] `Object#inspect` accept a list of instance variables to display
2025-04-07 13:29 [ruby-core:121551] [Ruby Feature#21219] `Object#inspect` accept a list of instance variables to display byroot (Jean Boussier) via ruby-core
@ 2025-04-07 17:37 ` mame (Yusuke Endoh) via ruby-core
2025-04-07 18:17 ` [ruby-core:121559] " jeremyevans0 (Jeremy Evans) via ruby-core
` (7 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: mame (Yusuke Endoh) via ruby-core @ 2025-04-07 17:37 UTC (permalink / raw)
To: ruby-core; +Cc: mame (Yusuke Endoh)
Issue #21219 has been updated by mame (Yusuke Endoh).
Just FYI, pretty_print already has that mechanism. It allows to control the list of instance variables that should be displayed by defining a method named `pretty_print_instance_variables`.
```ruby
class Foo
def initialize
@pub_1 = :A
@pub_2 = :B
@priv_1 = :secret
@priv_2 = :secret
end
def pretty_print_instance_variables
super - [:@priv_1, :@priv_2]
end
end
pp Foo.new #=> #<Foo:0x00007f319f5077b0 @pub_1=:A, @pub_2=:B>
```
I am not sure how to achieve this with `#inspect`. Should we introduce a method like `#inspect_instance_variables` or something to do the same protocol?
I don't think the keyword argument in `#inspect` is a very good API because specifying the ivar name list outside of the class definition looks a bit unconfortable.
----------------------------------------
Feature #21219: `Object#inspect` accept a list of instance variables to display
https://bugs.ruby-lang.org/issues/21219#change-112584
* 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/
^ permalink raw reply [flat|nested] 10+ messages in thread
* [ruby-core:121559] [Ruby Feature#21219] `Object#inspect` accept a list of instance variables to display
2025-04-07 13:29 [ruby-core:121551] [Ruby Feature#21219] `Object#inspect` accept a list of instance variables to display byroot (Jean Boussier) via ruby-core
2025-04-07 17:37 ` [ruby-core:121558] " mame (Yusuke Endoh) via ruby-core
@ 2025-04-07 18:17 ` jeremyevans0 (Jeremy Evans) via ruby-core
2025-04-07 18:34 ` [ruby-core:121562] " byroot (Jean Boussier) via ruby-core
` (6 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: jeremyevans0 (Jeremy Evans) via ruby-core @ 2025-04-07 18:17 UTC (permalink / raw)
To: ruby-core; +Cc: jeremyevans0 (Jeremy Evans)
Issue #21219 has been updated by jeremyevans0 (Jeremy Evans).
I agree with @mame that a keyword argument to `#inspect` is undesirable. `#inspect_instance_variables` is one possible approach. Another possible approach:
```ruby
private def inspect_include_variable?(ivar)
ivar != :@priv_1 && ivar != :@priv2
end
```
`#inspect` would call this method with each ivar, and not include the ivar if it returned false. The default implementation would return true for all ivars. This could be optimized so that it checks whether the object responds to the method, and if not, it assumes it would return true without attempting to call it.
----------------------------------------
Feature #21219: `Object#inspect` accept a list of instance variables to display
https://bugs.ruby-lang.org/issues/21219#change-112585
* 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/
^ permalink raw reply [flat|nested] 10+ messages in thread
* [ruby-core:121562] [Ruby Feature#21219] `Object#inspect` accept a list of instance variables to display
2025-04-07 13:29 [ruby-core:121551] [Ruby Feature#21219] `Object#inspect` accept a list of instance variables to display byroot (Jean Boussier) via ruby-core
2025-04-07 17:37 ` [ruby-core:121558] " mame (Yusuke Endoh) via ruby-core
2025-04-07 18:17 ` [ruby-core:121559] " jeremyevans0 (Jeremy Evans) via ruby-core
@ 2025-04-07 18:34 ` byroot (Jean Boussier) via ruby-core
2025-04-07 18:41 ` [ruby-core:121563] " bkuhlmann (Brooke Kuhlmann) via ruby-core
` (5 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: byroot (Jean Boussier) via ruby-core @ 2025-04-07 18:34 UTC (permalink / raw)
To: ruby-core; +Cc: byroot (Jean Boussier)
Issue #21219 has been updated by byroot (Jean Boussier).
I'm fine with either of those, with perhaps a slight preference for `private def inspect_instance_variables = [:@a, :@b]`.
----------------------------------------
Feature #21219: `Object#inspect` accept a list of instance variables to display
https://bugs.ruby-lang.org/issues/21219#change-112587
* 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/
^ permalink raw reply [flat|nested] 10+ messages in thread
* [ruby-core:121563] [Ruby Feature#21219] `Object#inspect` accept a list of instance variables to display
2025-04-07 13:29 [ruby-core:121551] [Ruby Feature#21219] `Object#inspect` accept a list of instance variables to display byroot (Jean Boussier) via ruby-core
` (2 preceding siblings ...)
2025-04-07 18:34 ` [ruby-core:121562] " byroot (Jean Boussier) via ruby-core
@ 2025-04-07 18:41 ` bkuhlmann (Brooke Kuhlmann) via ruby-core
2025-04-07 18:44 ` [ruby-core:121564] " byroot (Jean Boussier) via ruby-core
` (4 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: bkuhlmann (Brooke Kuhlmann) via ruby-core @ 2025-04-07 18:41 UTC (permalink / raw)
To: ruby-core; +Cc: bkuhlmann (Brooke Kuhlmann)
Issue #21219 has been updated by bkuhlmann (Brooke Kuhlmann).
>From an developer ergonomic standoint, could only symbols be used to simplify the syntax further? Example:
``` ruby
# First suggestion.
def inspect = super(:host, :user)
# Third suggestion.
private def inspect_instance_variables = %i[a b].
```
----------------------------------------
Feature #21219: `Object#inspect` accept a list of instance variables to display
https://bugs.ruby-lang.org/issues/21219#change-112588
* 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/
^ permalink raw reply [flat|nested] 10+ messages in thread
* [ruby-core:121564] [Ruby Feature#21219] `Object#inspect` accept a list of instance variables to display
2025-04-07 13:29 [ruby-core:121551] [Ruby Feature#21219] `Object#inspect` accept a list of instance variables to display byroot (Jean Boussier) via ruby-core
` (3 preceding siblings ...)
2025-04-07 18:41 ` [ruby-core:121563] " bkuhlmann (Brooke Kuhlmann) via ruby-core
@ 2025-04-07 18:44 ` byroot (Jean Boussier) via ruby-core
2025-04-10 9:39 ` [ruby-core:121619] " nobu (Nobuyoshi Nakada) via ruby-core
` (3 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: byroot (Jean Boussier) via ruby-core @ 2025-04-07 18:44 UTC (permalink / raw)
To: ruby-core; +Cc: byroot (Jean Boussier)
Issue #21219 has been updated by byroot (Jean Boussier).
> could only symbols be used to simplify the syntax further?
Technically possible, but not ideal because of various implementation details (instance variables without a `@` prefix exist internally).
But regardless, it's also not good because then you can't use the values returned by `Object#instance_variables`.
----------------------------------------
Feature #21219: `Object#inspect` accept a list of instance variables to display
https://bugs.ruby-lang.org/issues/21219#change-112589
* 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/
^ permalink raw reply [flat|nested] 10+ messages in thread
* [ruby-core:121619] [Ruby Feature#21219] `Object#inspect` accept a list of instance variables to display
2025-04-07 13:29 [ruby-core:121551] [Ruby Feature#21219] `Object#inspect` accept a list of instance variables to display byroot (Jean Boussier) via ruby-core
` (4 preceding siblings ...)
2025-04-07 18:44 ` [ruby-core:121564] " byroot (Jean Boussier) via ruby-core
@ 2025-04-10 9:39 ` nobu (Nobuyoshi Nakada) via ruby-core
2025-05-11 17:02 ` [ruby-core:122021] " bkuhlmann (Brooke Kuhlmann) via ruby-core
` (2 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: nobu (Nobuyoshi Nakada) via ruby-core @ 2025-04-10 9:39 UTC (permalink / raw)
To: ruby-core; +Cc: nobu (Nobuyoshi Nakada)
Issue #21219 has been updated by nobu (Nobuyoshi Nakada).
https://github.com/nobu/ruby/tree/inspect_instance_variables
----------------------------------------
Feature #21219: `Object#inspect` accept a list of instance variables to display
https://bugs.ruby-lang.org/issues/21219#change-112673
* 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/
^ permalink raw reply [flat|nested] 10+ messages in thread
* [ruby-core:122021] [Ruby Feature#21219] `Object#inspect` accept a list of instance variables to display
2025-04-07 13:29 [ruby-core:121551] [Ruby Feature#21219] `Object#inspect` accept a list of instance variables to display byroot (Jean Boussier) via ruby-core
` (5 preceding siblings ...)
2025-04-10 9:39 ` [ruby-core:121619] " nobu (Nobuyoshi Nakada) via ruby-core
@ 2025-05-11 17:02 ` bkuhlmann (Brooke Kuhlmann) via ruby-core
2025-06-05 6:57 ` [ruby-core:122428] " matz (Yukihiro Matsumoto) via ruby-core
2025-06-07 8:07 ` [ruby-core:122490] " byroot (Jean Boussier) via ruby-core
8 siblings, 0 replies; 10+ messages in thread
From: bkuhlmann (Brooke Kuhlmann) via ruby-core @ 2025-05-11 17:02 UTC (permalink / raw)
To: ruby-core; +Cc: bkuhlmann (Brooke Kuhlmann)
Issue #21219 has been updated by bkuhlmann (Brooke Kuhlmann).
ℹ️ In case it's of interest/inspiration, I released [Inspectable](https://alchemists.io/projects/inspectable) 0.2.0 which tackles what is described in this issue by making it easy to redact sensitive information, slim down verbosity, or remove an instance variable entirely from the output.
----------------------------------------
Feature #21219: `Object#inspect` accept a list of instance variables to display
https://bugs.ruby-lang.org/issues/21219#change-113139
* 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/
^ permalink raw reply [flat|nested] 10+ messages in thread
* [ruby-core:122428] [Ruby Feature#21219] `Object#inspect` accept a list of instance variables to display
2025-04-07 13:29 [ruby-core:121551] [Ruby Feature#21219] `Object#inspect` accept a list of instance variables to display byroot (Jean Boussier) via ruby-core
` (6 preceding siblings ...)
2025-05-11 17:02 ` [ruby-core:122021] " bkuhlmann (Brooke Kuhlmann) via ruby-core
@ 2025-06-05 6:57 ` matz (Yukihiro Matsumoto) via ruby-core
2025-06-07 8:07 ` [ruby-core:122490] " byroot (Jean Boussier) via ruby-core
8 siblings, 0 replies; 10+ messages in thread
From: matz (Yukihiro Matsumoto) via ruby-core @ 2025-06-05 6:57 UTC (permalink / raw)
To: ruby-core; +Cc: matz (Yukihiro Matsumoto)
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/
^ permalink raw reply [flat|nested] 10+ messages in thread
* [ruby-core:122490] [Ruby Feature#21219] `Object#inspect` accept a list of instance variables to display
2025-04-07 13:29 [ruby-core:121551] [Ruby Feature#21219] `Object#inspect` accept a list of instance variables to display byroot (Jean Boussier) via ruby-core
` (7 preceding siblings ...)
2025-06-05 6:57 ` [ruby-core:122428] " matz (Yukihiro Matsumoto) via ruby-core
@ 2025-06-07 8:07 ` byroot (Jean Boussier) via ruby-core
8 siblings, 0 replies; 10+ messages in thread
From: byroot (Jean Boussier) via ruby-core @ 2025-06-07 8:07 UTC (permalink / raw)
To: ruby-core; +Cc: byroot (Jean Boussier)
Issue #21219 has been updated by byroot (Jean Boussier).
Thank you Matz.
I opened a pull request derived from @nobu's patch: https://github.com/ruby/ruby/pull/13555
I think we can merge it soon, and if someone comes up with a better name it's fairly easy to rename.
----------------------------------------
Feature #21219: `Object#inspect` accept a list of instance variables to display
https://bugs.ruby-lang.org/issues/21219#change-113679
* 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/
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2025-06-07 8:08 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-04-07 13:29 [ruby-core:121551] [Ruby Feature#21219] `Object#inspect` accept a list of instance variables to display byroot (Jean Boussier) via ruby-core
2025-04-07 17:37 ` [ruby-core:121558] " mame (Yusuke Endoh) via ruby-core
2025-04-07 18:17 ` [ruby-core:121559] " jeremyevans0 (Jeremy Evans) via ruby-core
2025-04-07 18:34 ` [ruby-core:121562] " byroot (Jean Boussier) via ruby-core
2025-04-07 18:41 ` [ruby-core:121563] " bkuhlmann (Brooke Kuhlmann) via ruby-core
2025-04-07 18:44 ` [ruby-core:121564] " byroot (Jean Boussier) via ruby-core
2025-04-10 9:39 ` [ruby-core:121619] " nobu (Nobuyoshi Nakada) via ruby-core
2025-05-11 17:02 ` [ruby-core:122021] " bkuhlmann (Brooke Kuhlmann) via ruby-core
2025-06-05 6:57 ` [ruby-core:122428] " matz (Yukihiro Matsumoto) via ruby-core
2025-06-07 8:07 ` [ruby-core:122490] " byroot (Jean Boussier) via ruby-core
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).