Actions
Bug #19597
closedProcess.argv0 returns the same mutable String
Description
Process.argv0
returns a name of the script being executed. But it seems it's always the same object, that can be modified and this change will be visible to the consequent Process.argv0
calls.
It makes sense to return a frozen String that cannot be modified.
$0
initial value equals a value returned by Process.argv0
so $0
by default also could be a frozen String.
Example:
puts "Object id:"
p Process.argv0.object_id
p Process.argv0.object_id
puts "Value before modification:"
p Process.argv0
Process.argv0.upcase!
puts "Value after modification:"
p Process.argv0
puts "$0 after modification:"
p $0
It will output:
Object id:
60
60
Value before modification:
"argv0.rb"
Value after modification:
"ARGV0.RB"
$0 after modification:
"ARGV0.RB"
Updated by andrykonchin (Andrew Konchin) over 1 year ago
- Description updated (diff)
Updated by nobu (Nobuyoshi Nakada) over 1 year ago
Updated by nobu (Nobuyoshi Nakada) over 1 year ago
I found $0
had been frozen till 1.9.
$ ruby1.9 -e 'p $0.upcase!; p $0; system("ps #$$")'
-e:1:in `upcase!': can't modify frozen String (RuntimeError)
from -e:1:in `<main>'
I'm not sure why/when it changed, yet.
Updated by nobu (Nobuyoshi Nakada) over 1 year ago
Once assigned to $0
, it is frozen.
$ ruby -v -e '$0 = "test"; p $0; $0.upcase!'
ruby 3.3.0dev (2023-05-05T14:01:50Z master 40438fc4d3) [x86_64-darwin22]
"test"
-e:1:in `upcase!': can't modify frozen String: "test" (FrozenError)
from -e:1:in `<main>'
So I'd say that only the first $0
is not frozen is a bug.
Updated by nobu (Nobuyoshi Nakada) over 1 year ago
It's 8c9a453f2d8a.
ruby_set_script_name
makes an unfrozen copy.
Updated by nobu (Nobuyoshi Nakada) over 1 year ago
- Tracker changed from Misc to Bug
- Backport set to 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN
Updated by nobu (Nobuyoshi Nakada) over 1 year ago
- Status changed from Open to Closed
Actions
Like0
Like0Like1Like0Like0Like0Like0Like0