ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:120696] [Ruby master Bug#21040] String#next! method does not mutate $& variable
@ 2025-01-15 13:07 radarek via ruby-core
  2025-01-15 14:49 ` [ruby-core:120697] " Eregon (Benoit Daloze) via ruby-core
  2025-01-15 14:50 ` [ruby-core:120698] " Hanmac (Hans Mackowiak) via ruby-core
  0 siblings, 2 replies; 3+ messages in thread
From: radarek via ruby-core @ 2025-01-15 13:07 UTC (permalink / raw)
  To: ruby-core; +Cc: radarek

Issue #21040 has been reported by radarek (Radosław Bułat).

----------------------------------------
Bug #21040: String#next! method does not mutate $& variable
https://bugs.ruby-lang.org/issues/21040

* Author: radarek (Radosław Bułat)
* Status: Open
* ruby -v: ruby 3.4.1 (2024-12-25 revision 48d4efcb85) +PRISM [arm64-darwin24]
* Backport: 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN
----------------------------------------
`String#next!` method should mutate string and return mutated version. For some reason, using it on `$&` does not mutate it but still returns new version.

Steps to reproduce (2 different ways):


```ruby
"123".gsub(/./){$&.next!}    # returns "234" as expected
"123".gsub(/./){$&.next!;$&} # returns "123" but should be "234"
"123".gsub(/./){_1.next!;_1} # returns "234" as expected



"123"[/./]
puts $&.      # prints 1
puts $&.next! # prints 2
puts $&       # prints 1 but should be 2
```




-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

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

* [ruby-core:120697] [Ruby master Bug#21040] String#next! method does not mutate $& variable
  2025-01-15 13:07 [ruby-core:120696] [Ruby master Bug#21040] String#next! method does not mutate $& variable radarek via ruby-core
@ 2025-01-15 14:49 ` Eregon (Benoit Daloze) via ruby-core
  2025-01-15 14:50 ` [ruby-core:120698] " Hanmac (Hans Mackowiak) via ruby-core
  1 sibling, 0 replies; 3+ messages in thread
From: Eregon (Benoit Daloze) via ruby-core @ 2025-01-15 14:49 UTC (permalink / raw)
  To: ruby-core; +Cc: Eregon (Benoit Daloze)

Issue #21040 has been updated by Eregon (Benoit Daloze).

Status changed from Open to Rejected

`$&` returns a new String on every usage, so this is fully expected:
```
$ ruby -e '"a" =~ /a/; p $&.object_id; p $&.object_id' 
60
80
```

----------------------------------------
Bug #21040: String#next! method does not mutate $& variable
https://bugs.ruby-lang.org/issues/21040#change-111532

* Author: radarek (Radosław Bułat)
* Status: Rejected
* ruby -v: ruby 3.4.1 (2024-12-25 revision 48d4efcb85) +PRISM [arm64-darwin24]
* Backport: 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN
----------------------------------------
`String#next!` method should mutate string and return mutated version. For some reason, using it on `$&` does not mutate it but still returns new version.

Steps to reproduce (2 different ways):


```ruby
"123".gsub(/./){$&.next!}    # returns "234" as expected
"123".gsub(/./){$&.next!;$&} # returns "123" but should be "234"
"123".gsub(/./){_1.next!;_1} # returns "234" as expected



"123"[/./]
puts $&.      # prints 1
puts $&.next! # prints 2
puts $&       # prints 1 but should be 2
```




-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

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

* [ruby-core:120698] [Ruby master Bug#21040] String#next! method does not mutate $& variable
  2025-01-15 13:07 [ruby-core:120696] [Ruby master Bug#21040] String#next! method does not mutate $& variable radarek via ruby-core
  2025-01-15 14:49 ` [ruby-core:120697] " Eregon (Benoit Daloze) via ruby-core
@ 2025-01-15 14:50 ` Hanmac (Hans Mackowiak) via ruby-core
  1 sibling, 0 replies; 3+ messages in thread
From: Hanmac (Hans Mackowiak) via ruby-core @ 2025-01-15 14:50 UTC (permalink / raw)
  To: ruby-core; +Cc: Hanmac (Hans Mackowiak)

Issue #21040 has been updated by Hanmac (Hans Mackowiak).


These are called `virtual variables`, a new object is created each time you try to access it

```c
    rb_define_virtual_variable("$~", get_LAST_MATCH_INFO, match_setter);
    rb_define_virtual_variable("$&", last_match_getter, 0);
    rb_define_virtual_variable("$`", prematch_getter, 0);
    rb_define_virtual_variable("$'", postmatch_getter, 0);
    rb_define_virtual_variable("$+", last_paren_match_getter, 0);
```

in ruby this is the result:
```ruby

"123".gsub(/.../){
  p [$&, 
     $&.object_id, #=> 30
     $&.object_id  #=> 40
  ]
}
```

----------------------------------------
Bug #21040: String#next! method does not mutate $& variable
https://bugs.ruby-lang.org/issues/21040#change-111533

* Author: radarek (Radosław Bułat)
* Status: Rejected
* ruby -v: ruby 3.4.1 (2024-12-25 revision 48d4efcb85) +PRISM [arm64-darwin24]
* Backport: 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN
----------------------------------------
`String#next!` method should mutate string and return mutated version. For some reason, using it on `$&` does not mutate it but still returns new version.

Steps to reproduce (2 different ways):


```ruby
"123".gsub(/./){$&.next!}    # returns "234" as expected
"123".gsub(/./){$&.next!;$&} # returns "123" but should be "234"
"123".gsub(/./){_1.next!;_1} # returns "234" as expected



"123"[/./]
puts $&.      # prints 1
puts $&.next! # prints 2
puts $&       # prints 1 but should be 2
```




-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

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

end of thread, other threads:[~2025-01-15 14:51 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-01-15 13:07 [ruby-core:120696] [Ruby master Bug#21040] String#next! method does not mutate $& variable radarek via ruby-core
2025-01-15 14:49 ` [ruby-core:120697] " Eregon (Benoit Daloze) via ruby-core
2025-01-15 14:50 ` [ruby-core:120698] " Hanmac (Hans Mackowiak) via ruby-core

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 NNTP newsgroup(s).