What is the variable ARGV in ruby?

From what I read, ARGV should be constant, since it is all uppercase, but I was able to write a quick program that changed one of the values ​​in ARGV without errors. So what is the type of ARGV variable?

p ARGV
ARGV[0] = "Not the orginal"
p ARGV
+3
source share
2 answers

ARGV is a constant, but it is an array. The values ​​in a constant array can be freely changed without any warning, like any ordinary element of an array.

irb(main)> ARGV.class
=> Array
irb(main)> QWERTY = [1, 2, 3, 4]
=> [1, 2, 3, 4]
irb(main)> QWERTY[1] = 5
=> 5
irb(main)> QWERTY
=> [1, 5, 3, 4]
irb(main)> QWERTY << 6
=> [1, 5, 3, 4, 6]
irb(main)> QWERTY = 3
(irb): warning: already initialized constant QWERTY
=> 3
+6
source

ARGV - . , "" , , , . , const ++. Ruby. ( , " " Ruby. , . .)

:

ruby-1.9.2-p0 > CONSTANT = [1,2,3]
 => [1, 2, 3] 
ruby-1.9.2-p0 > frozen = [1,2,3].freeze
 => [1, 2, 3] 
ruby-1.9.2-p0 > CONSTANT << 4
 => [1, 2, 3, 4] 
ruby-1.9.2-p0 > frozen << 4
RuntimeError: can't modify frozen array
+7

Source: https://habr.com/ru/post/1771037/


All Articles