Project

General

Profile

Actions

Feature #19027

closed

.= syntax

Added by jeromedalbert (Jerome Dalbert) 2 months ago. Updated 2 months ago.

Status:
Rejected
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:110122]

Description

I wish I could do this in Ruby:

records .= where.not(id: excluded_ids) if some_condition

instead of:

records = records.where.not(id: excluded_ids) if some_condition

We already have +=, -=, ||=, etc, so why not have a .= syntax?

I rarely need this since most of the time self replacement methods like gsub! are available. Over my many years of Ruby programming I wished I could use a .= syntax maybe a handful of times, so this would be a rarely useful feature, but I find it to be quite elegant in the rare cases it could be needed.

Maybe this is just me being weird but I thought I would share.


Related issues 1 (1 open0 closed)

Is duplicate of Ruby master - Feature #6841: Shorthand for Assigning Return Value of Method to SelfAssignedmatz (Yukihiro Matsumoto)Actions

Updated by hmdne (hmdne -) 2 months ago

I like it, though one while reading that code could have a problem with attempting to find a where variable.

Perhaps a more clearer way to write this would be the following:

records.=where.not(id: excluded_ids) if some_condition

We also have a &. operator, perhaps we should see .= in a similar fashion.

Actions #2

Updated by mame (Yusuke Endoh) 2 months ago

  • Is duplicate of Feature #6841: Shorthand for Assigning Return Value of Method to Self added

Updated by mame (Yusuke Endoh) 2 months ago

  • Status changed from Open to Rejected

If I remember correctly, this has been proposed several times. I found one ticket #6841, so I'll close this as a duplicate.

Updated by austin (Austin Ziegler) 2 months ago

jeromedalbert (Jerome Dalbert) wrote:

I wish I could do this in Ruby:

records .= where.not(id: excluded_ids) if some_condition

instead of:

records = records.where.not(id: excluded_ids) if some_condition

I find that both unreadable and undecipherable, personally. This is one case where something like a pipeline operator might be better (https://bugs.ruby-lang.org/issues/15799).

records =
  RecordSource
  |> where() # original condition
  |> maybe_exclude(some_condition, excluded_ids)



def maybe_exclude(records, condition, excluded_ids)
  condition ? records.where.not(id: excluded_ids) : records
end

It’s not shorter, but it allows for a better reading of the query build in the first place. It might be possible to do something like that using Kernel#then:

records =
  RecordSource
    .where() # original condition
    .then { maybe_exclude(_1, some_condition, excluded_ids) }



def maybe_exclude(records, condition, excluded_ids)
  condition ? records.where.not(id: excluded_ids) : records
end

We already have +=, -=, ||=, etc, so why not have a .= syntax?

This would be different than the others, because += &c. result in method calls on the receiver. If we wanted something clearer, maybe records = &..when.not… where &.. would be a signal to "retrieve the previous variable symbol to use here".

Updated by Dan0042 (Daniel DeLorme) 2 months ago

I don't think that works. var += expr is a shortcut for var = var.+(expr). But var .= method translates to var = var.method; the right-hand side is not an expression, so it's a very different kind of construct.

But I'm a fan of the "fluent interface" style, so I wouldn't mind records = .where.not(id: excluded_ids)
:-)

(I would also love fluent-style
foo && .bar
foo and .bar
foo{ .bar } but #16120 has been rejected once so I don't think there's much hope.)

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0