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.
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)…defmaybe_exclude(records,condition,excluded_ids)condition?records.where.not(id: excluded_ids):recordsend
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)}…defmaybe_exclude(records,condition,excluded_ids)condition?records.where.not(id: excluded_ids):recordsend
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".
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.)