Actions
Bug #21940
openRuby::Box: `$_` returns stale value due to gvar_tbl caching
Bug #21940:
Ruby::Box: `$_` returns stale value due to gvar_tbl caching
Description
Environment¶
- Ruby Version v4.0.1
- OS: macOS 15.6.1 (arm64)
-
RUBY_BOX=1enabled
Reproduction¶
echo -e "a\nb" | RUBY_BOX=1 ruby -e 'gets; $_; gets; p $_'
Expected¶
"b\n"
The second gets reads "b" and sets it to $_. p $_ should return the latest value.
Actual¶
"a\n"
$_ returns the stale value from the first gets.
Root Cause¶
$_ is a special variable stored in svar. Its getter (rb_lastline_get) reads the current value from svar each time it is called.
However, when Box is enabled, I believe rb_gvar_get() caches the value in the Box's gvar_tbl on first access.
On subsequent reads, it returns the cached value directly without calling the getter.
Since gets updates $_ via rb_lastline_set() which bypasses rb_gvar_set(), the cache becomes stale.
Updated by dak2 (Daichi Kamiyama) about 18 hours ago
ยท Edited
Updated by nobu (Nobuyoshi Nakada) about 7 hours ago
- Tags set to box
- Assignee set to tagomoris (Satoshi Tagomori)
- Backport changed from 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN, 4.0: UNKNOWN to 3.2: DONTNEED, 3.3: DONTNEED, 3.4: DONTNEED, 4.0: REQUIRED
Actions