Project

General

Profile

Actions

Bug #21635

closed

Reading code from STDIN unexpectedly sets $_ in 3.4.6+

Added by sisyphus (Sisyphus P) 1 day ago. Updated about 2 hours 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)

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0