[ruby-core:114968] [Ruby master Feature#19884] Make Safe Navigation Operator work on classes
From:
"p8 (Petrik de Heus) via ruby-core" <ruby-core@...>
Date:
2023-10-07 09:50:11 UTC
List:
ruby-core #114968
Issue #19884 has been updated by p8 (Petrik de Heus).
There's multiple examples in the Rails code base:
https://github.com/rails/rails/blob/7f7f9df8641e35a076fe26bd097f6a1b22cb4e2d/railties/lib/rails/generators/named_base.rb#L139
https://github.com/rails/rails/blob/7f7f9df8641e35a076fe26bd097f6a1b22cb4e2d/railties/lib/rails/generators/generated_attribute.rb#L67C1-L76
```ruby
def valid_type?(type)
DEFAULT_TYPES.include?(type.to_s) ||
!defined?(ActiveRecord::Base) ||
ActiveRecord::Base.connection.valid_type?(type)
end
```
It would be nice to rewrite this as:
```ruby
def valid_type?(type)
DEFAULT_TYPES.include?(type.to_s) ||
ActiveRecord&::Base&.connection.valid_type?(type)
end
```
----------------------------------------
Feature #19884: Make Safe Navigation Operator work on classes
https://bugs.ruby-lang.org/issues/19884#change-104840
* Author: p8 (Petrik de Heus)
* Status: Open
* Priority: Normal
----------------------------------------
If a class might not be defined we need to add a conditional:
```ruby
defined?(ActiveRecord::Base) && ActiveRecord::Base.some_method
```
It would be nice if we could use the Safe Navigation Operator instead.
```ruby
ActiveRecord::Base&.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/