From: "kddnewton (Kevin Newton) via ruby-core" Date: 2023-10-10T14:01:31+00:00 Subject: [ruby-core:114993] [Ruby master Feature#19884] Make Safe Navigation Operator work on classes Issue #19884 has been updated by kddnewton (Kevin Newton). @nobu has said this already, but just to reiterate, if we changed `Foo&.bar` to do nothing in the case that it was undefined, it would break things like zeitwerk and other libraries that handle const_missing. ---------------------------------------- Feature #19884: Make Safe Navigation Operator work on classes https://bugs.ruby-lang.org/issues/19884#change-104863 * Author: p8 (Petrik de Heus) * Status: Open * Priority: Normal ---------------------------------------- If a constant isn't defined it will raise a NameError: ```ruby DoesNotExist.some_method # raises: uninitialized constant DoesNotExist (NameError) ``` In libraries that have optional dependencies, we can check if the constant is defined before calling a method on it: ```ruby defined?(OptionalDependency) && OptionalDependency.some_method ``` Currently in Ruby, the Safe Navigation Operator is used to avoid NoMethodError exceptions when calling methods on objects that may be nil. It would be nice if we could use the Safe Navigation Operator to avoid NameError on undefined constants as well. ```ruby ClassThatMightNotExist&.some_method ``` -- 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/postorius/lists/ruby-core.ml.ruby-lang.org/