Project

General

Profile

Feature #11537

Introduce "Safe navigation operator"

Added by hsbt (Hiroshi SHIBATA) about 5 years ago. Updated almost 5 years ago.

Status:
Closed
Priority:
Normal
Target version:
-
[ruby-core:<unknown>]

Description

I sometimes write following code with rails application:

u = User.find(id)
if u && u.profile && u.profile.thumbnails && u.profiles.thumbnails.large
  ...

or

# Use ActiveSupport
if u.try!(:profile).try!(:thumbnails).try!(:large)
 ...

I hope to write shortly above code. Groovy has above operator named "Safe navigation operator" with "?." syntax.
Ruby can't use "?." operator.

Can we use ".?" syntax. like this:

u = User.find(id)
u.?profile.?thumbnails.?large

Matz. How do you think about this?


Related issues

Related to Ruby master - Feature #8191: Short-hand syntax for duck-typingClosedmatz (Yukihiro Matsumoto)Actions
Related to Ruby master - Feature #8237: Logical method chaining via inferred receiverClosedActions
Related to Ruby master - Feature #11034: Nil ConditionalClosedActions
Related to Ruby master - Feature #1122: request for: Object#tryRejectedauthorNari (Narihiro Nakamura)02/07/2009Actions

Also available in: Atom PDF