Project

General

Profile

Actions

Bug #21635

closed

Reading code from STDIN unexpectedly sets $_ in 3.4.6+

Bug #21635: Reading code from STDIN unexpectedly sets $_ in 3.4.6+

Added by sisyphus (Sisyphus P) 22 days ago. Updated 21 days ago.

Status:
Closed
Assignee:
Target version:
-
ruby -v:
ruby 3.4.6 (2025-09-16 revision dbd83256b1) +PRISM [arm64-darwin23]
[ruby-core:<unknown>]

Description

It seems from Ruby 3.4.6+ if you pass in code that doesn't end with a newline to the ruby interpreter it sets $_:

# ruby -v
ruby 3.4.6 (2025-09-16 revision dbd83256b1) +PRISM [arm64-darwin23]
# printf 'p $_' | ruby
"p $_"

This only happens if the code passed in doesn't end with a newline and doesn't happen in ruby 3.4.5 or earlier:

# ruby -v
ruby 3.4.5 (2025-07-16 revision 20cda200d3) +PRISM [arm64-darwin23]
# printf 'p $_' | ruby
nil

I would expect that $_ is nil at the start of program execution.

NB. this does result in the world's shortest Ruby quine (printf 'print' | ruby)

Updated by kddnewton (Kevin Newton) 22 days ago Actions #1

  • Assignee set to prism

Updated by kddnewton (Kevin Newton) 22 days ago Actions #2

This is happening because of prism's use of IO#gets on STDIN. We will fix this.

Updated by kddnewton (Kevin Newton) 21 days ago Actions #3

  • Status changed from Open to Closed

Applied in changeset git|d036dc0a794d87309f912e7585749c681c2438f5.


For prism parser, do not update $_ from STDIN

Fixes [Bug #21635]

Actions

Also available in: PDF Atom