Project

General

Profile

Actions

Bug #21558

closed

Regexp.union with multiple regexps returns a non-shareable object (should be shareable if possible)

Added by niku (niku _) 16 days ago. Updated 14 days ago.

Status:
Feedback
Assignee:
-
Target version:
-
ruby -v:
ruby 3.5.0dev (2025-08-30T18:24:25Z master 5c7dfe85a1) +PRISM [arm64-darwin24]
[ruby-core:123141]

Description

Regexp.union returns a non-shareable object when given multiple Regex arguments. Is this expected behavior?
It would be preferable if the resulting Regexp were shareable, since single-argument Regexp.union does produce a shareable object.

irb(main):001> a = /a/
=> /a/
irb(main):002> b = Regexp.union(a)
=> /a/
irb(main):003> c = Regexp.union(a, a)
=> /(?-mix:a)|(?-mix:a)/
irb(main):004> Ractor.shareable?(a)
=> true
irb(main):005> Ractor.shareable?(b)
=> true
irb(main):006> Ractor.shareable?(c)
=> false

Related issues 2 (1 open1 closed)

Related to Ruby - Feature #17256: Freeze all Regexp objectsClosedmatz (Yukihiro Matsumoto)Actions
Related to Ruby - Feature #8948: Frozen regexAssignedEregon (Benoit Daloze)Actions

Updated by nobu (Nobuyoshi Nakada) 16 days ago

  • Status changed from Open to Feedback

Regexp.union creates a new instance, except for a single Regexp argument case.
And it is impossible to revert a Ractor-shareable object to non-Ractor-shareable.
So it feels natural that the new instance is not Ractor-shareable as well as String.new and others, to me.

Updated by nobu (Nobuyoshi Nakada) 16 days ago

Another example is Range.
All instances of Range are automatically frozen so they are Ractor-shareable.
Personally, I think freezing all Regexp instances also may be nice, but that's a different story than Regexp.union.

Updated by niku (niku _) 15 days ago

Regexp.union creates a new instance, except for a single Regexp argument case.

Ah, that makes perfect sense now. Thank you for the detailed explanation.

irb(main):001> a = /a/
=> /a/
irb(main):002> a.__id__
=> 7232
irb(main):003> b = Regexp.union(a)
=> /a/
irb(main):004> b.__id__
=> 7232
irb(main):005> c = Regexp.union(a, a)
=> /(?-mix:a)|(?-mix:a)/
irb(main):006> c.__id__
=> 17912

Personally, I think freezing all Regexp instances also may be nice, but that's a different story than Regexp.union.

I agree with your opinion. I'll create a new issue.

Updated by niku (niku _) 15 days ago

I've found an issue that already talked about it https://bugs.ruby-lang.org/issues/17256.

Actions #5

Updated by znz (Kazuhiro NISHIYAMA) 14 days ago

Actions #6

Updated by Eregon (Benoit Daloze) 14 days ago

Updated by Eregon (Benoit Daloze) 14 days ago

There is also #8948. I didn't get time to try it, but anyone is welcome to try and find if there are any failures in CI/the test suites and if so how to fix them.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0