Project

General

Profile

Actions

Feature #19884

open

Make Safe Navigation Operator work on classes

Added by p8 (Petrik de Heus) over 1 year ago. Updated about 1 year ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:114774]

Description

If a constant isn't defined it will raise a NameError:

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:

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.

ClassThatMightNotExist&.some_method

Actions

Also available in: Atom PDF

Like0
Like2Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like1