Project

General

Profile

Actions

Feature #11537

closed

Introduce "Safe navigation operator"

Added by hsbt (Hiroshi SHIBATA) over 8 years ago. Updated over 8 years ago.

Status:
Closed
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 4 (0 open4 closed)

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
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0