Project

General

Profile

Actions

Feature #11939

open

Syntax sugar to apply a method replace a variable

Added by sawa (Tsuyoshi Sawada) about 8 years ago. Updated about 8 years ago.

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

Description

There is frequent use case to modify the value of a variable and keep it referred to by the same variable name. When method chaining cannot be done (for example, when the method is conditionally called), the same variable name would have to be repeated:

var = ...
var = var.some_method(some_args) if some_condition
var = ...
...

I would like to propose a syntax sugar for this kind of situation. I have two options in mind.

(1) =.

var = ...
var =.some_method(some_args) if some_condition
var = ...
...

(2) .=

var = ...
var .= some_method(some_args) if some_condition
var = ...
...

Notation (2) seems to be in line with syntax sugar like +=, but option (1) has the advantage that the period comes together with the method name.

Updated by nobu (Nobuyoshi Nakada) about 8 years ago

Maybe an op assign could be used with (1)?

var +=.some_method(some_args)

Updated by codetodya (Sumit Mahamuni) about 8 years ago

According to ruby conventions shouldn't that be

var.some_method!(some_args) if some_condition.

Because you are modifying same object. Why do you need new feature?

Updated by sawa (Tsuyoshi Sawada) about 8 years ago

Nobuyoshi Nakada wrote:

Maybe an op assign could be used with (1)?

var +=.some_method(some_args)

That is interesting. It would be even more useful.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0