From: "mame (Yusuke Endoh) via ruby-core" Date: 2024-11-27T18:34:57+00:00 Subject: [ruby-core:120025] [Ruby master Feature#20912] Move warning when redefining object_id to __id__ Issue #20912 has been updated by mame (Yusuke Endoh). I think it's a good idea to warn against redefining `__id__`, but I think we should keep warning against redefining `object_id` as well. The reason is that it is relatively well known that "we should use `__send__` instead of `send` for unknown objects", but I think it is less well known that "we should use `__id__` instead of `object_id`". In fact, `object_id` is called far more often than `__id__`. ``` $ gem-codesearch \\.object_id | wc -l 24048 $ gem-codesearch \\.__id__ | wc -l 3763 ``` I don't know how many of these call object_id for instances of user-defined classes, but such gems may not work properly if `object_id` is redefined. If we really want to allow the redefinition of `object_id`, we will need to create a consensus that a call to `object_id` for unknown objects is dangerous, and get people to fix gems so that `__id__` is used instead of `object_id` for unknown objects. However, if you are currently not in that much trouble, I guess it would be better to maintain the current status. --- Incidentally, the names `__id__` and `object_id` have an interesting history. * There used to be only `Object#id`. * `Object#__id__` was introduced to avoid the redefinition of the method name `id`. * `Object#object_id` was also introduced. * `Object#id` was completely removed to allow application authors to use tje name `id` freely. * And `__id__` and `object_id` were left. I asked matz why the new name `object_id` was introduced when `__id__` was already there. He answered, "I didn't want to have to always use `__id__` because it would be ugly". https://twitter.com/yukihiro_matz/status/1861726864332742995 Indeed, it's not very cool to call `__id__` on an object that you know you don't have to worry about redefining. ---------------------------------------- Feature #20912: Move warning when redefining object_id to __id__ https://bugs.ruby-lang.org/issues/20912#change-110765 * Author: jhawthorn (John Hawthorn) * Status: Open ---------------------------------------- Currently if you create a class and redefine or remove either `object_id` or `__send__` it will issue a warning. There's no such warning on `__id__`. ``` ��� ruby -we 'class << Object.new; def object_id = 1; end' -e:1: warning: redefining `object_id' may cause serious problems ��� ruby -we 'class << Object.new; def __id__ = 1; end' ��� ``` It makes sense that there's no warning on `send`, because we expect `__send__` to be the method reliably available. `__send__` is on `BasicObject`, `send` is only on `Kernel`. This seems a little inconsistent that `object_id` warns while `__id__` does not warn. `__id__` is the equivalent to `__send__` as it's also on `BasicObject`, where `object_id` is only on `Kernel`. This may be a more obvious problem in Ruby 3.4.0 as commit:cffd380390967d17899fde0a81f5151ae6ddd076 makes the warning print in more cases. I propose we change this warning to be emitted only when `__id__` is redefined and not when `object_id` is redefined: **Proposed behaviour:** ``` ��� ruby -we 'class << Object.new; def object_id = 1; end' ��� ruby -we 'class << Object.new; def __id__ = 1; end' -e:1: warning: redefining `__id__' may cause serious problems ��� ``` -- 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/