From: "austin (Austin Ziegler)" Date: 2022-09-28T14:32:21+00:00 Subject: [ruby-core:110126] [Ruby master Feature#19027] .= syntax Issue #19027 has been updated by austin (Austin Ziegler). 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). ```ruby 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`: ```ruby 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". ---------------------------------------- Feature #19027: .= syntax https://bugs.ruby-lang.org/issues/19027#change-99384 * Author: jeromedalbert (Jerome Dalbert) * Status: Rejected * Priority: Normal ---------------------------------------- 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. -- https://bugs.ruby-lang.org/ Unsubscribe: