ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:110122] [Ruby master Feature#19027] .= syntax
@ 2022-09-28 13:03 jeromedalbert (Jerome Dalbert)
  2022-09-28 13:30 ` [ruby-core:110123] " hmdne (hmdne -)
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: jeromedalbert (Jerome Dalbert) @ 2022-09-28 13:03 UTC (permalink / raw)
  To: ruby-core

Issue #19027 has been reported by jeromedalbert (Jerome Dalbert).

----------------------------------------
Feature #19027: .= syntax
https://bugs.ruby-lang.org/issues/19027

* Author: jeromedalbert (Jerome Dalbert)
* Status: Open
* 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/

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [ruby-core:110123] [Ruby master Feature#19027] .= syntax
  2022-09-28 13:03 [ruby-core:110122] [Ruby master Feature#19027] .= syntax jeromedalbert (Jerome Dalbert)
@ 2022-09-28 13:30 ` hmdne (hmdne -)
  2022-09-28 14:30 ` [ruby-core:110125] " mame (Yusuke Endoh)
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: hmdne (hmdne -) @ 2022-09-28 13:30 UTC (permalink / raw)
  To: ruby-core

Issue #19027 has been updated by hmdne (hmdne -).


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.

----------------------------------------
Feature #19027: .= syntax
https://bugs.ruby-lang.org/issues/19027#change-99379

* Author: jeromedalbert (Jerome Dalbert)
* Status: Open
* 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/

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [ruby-core:110125] [Ruby master Feature#19027] .= syntax
  2022-09-28 13:03 [ruby-core:110122] [Ruby master Feature#19027] .= syntax jeromedalbert (Jerome Dalbert)
  2022-09-28 13:30 ` [ruby-core:110123] " hmdne (hmdne -)
@ 2022-09-28 14:30 ` mame (Yusuke Endoh)
  2022-09-28 14:32 ` [ruby-core:110126] " austin (Austin Ziegler)
  2022-09-28 14:36 ` [ruby-core:110127] " Dan0042 (Daniel DeLorme)
  3 siblings, 0 replies; 5+ messages in thread
From: mame (Yusuke Endoh) @ 2022-09-28 14:30 UTC (permalink / raw)
  To: ruby-core

Issue #19027 has been updated by mame (Yusuke Endoh).

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.

----------------------------------------
Feature #19027: .= syntax
https://bugs.ruby-lang.org/issues/19027#change-99383

* 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/

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [ruby-core:110126] [Ruby master Feature#19027] .= syntax
  2022-09-28 13:03 [ruby-core:110122] [Ruby master Feature#19027] .= syntax jeromedalbert (Jerome Dalbert)
  2022-09-28 13:30 ` [ruby-core:110123] " hmdne (hmdne -)
  2022-09-28 14:30 ` [ruby-core:110125] " mame (Yusuke Endoh)
@ 2022-09-28 14:32 ` austin (Austin Ziegler)
  2022-09-28 14:36 ` [ruby-core:110127] " Dan0042 (Daniel DeLorme)
  3 siblings, 0 replies; 5+ messages in thread
From: austin (Austin Ziegler) @ 2022-09-28 14:32 UTC (permalink / raw)
  To: ruby-core

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/

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [ruby-core:110127] [Ruby master Feature#19027] .= syntax
  2022-09-28 13:03 [ruby-core:110122] [Ruby master Feature#19027] .= syntax jeromedalbert (Jerome Dalbert)
                   ` (2 preceding siblings ...)
  2022-09-28 14:32 ` [ruby-core:110126] " austin (Austin Ziegler)
@ 2022-09-28 14:36 ` Dan0042 (Daniel DeLorme)
  3 siblings, 0 replies; 5+ messages in thread
From: Dan0042 (Daniel DeLorme) @ 2022-09-28 14:36 UTC (permalink / raw)
  To: ruby-core

Issue #19027 has been updated by Dan0042 (Daniel DeLorme).


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.)

----------------------------------------
Feature #19027: .= syntax
https://bugs.ruby-lang.org/issues/19027#change-99385

* 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/

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-09-28 14:36 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-28 13:03 [ruby-core:110122] [Ruby master Feature#19027] .= syntax jeromedalbert (Jerome Dalbert)
2022-09-28 13:30 ` [ruby-core:110123] " hmdne (hmdne -)
2022-09-28 14:30 ` [ruby-core:110125] " mame (Yusuke Endoh)
2022-09-28 14:32 ` [ruby-core:110126] " austin (Austin Ziegler)
2022-09-28 14:36 ` [ruby-core:110127] " Dan0042 (Daniel DeLorme)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).