开发者

Duplicating array with frozen elements

开发者_运维百科I am trying to duplicate the ARGV array to remove the frozen status of each element but nothing I do seems to work.

All other posts I can find on the subject suggest that using .dup should work however it isn't. Here is how I have used it:

args = ARGV.dup
puts args[0].frozen?
    => true

i = 0
argv.each {|x| 
           args[i] = x.dup
           i += 1 }
puts args[0].frozen?
     => true

I have also tried using the assign operator and clone method in these scenarios.


I tried and got the following:

args = ARGV.dup
p args[0].frozen? #=> true

args.each_with_index {|arg,i| args[i] = arg.dup }
p args[0].frozen? #=> false

Basically this should suffice:

args = ARGV.map(&:dup)
p args[0].frozen? #=> false
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜