【发布时间】:2012-09-13 16:16:56
【问题描述】:
在这段代码中,我想根据代码提供的索引值迭代我的数组,比如一次我想迭代并获得 ARGV[1] 第二次 ARGV[3] 的输出。
假设
ARGV = ["-f","abc","-x","-p","wer"]
#!/usr/bin/env ruby
@lenght = ARGV.length
@factory_config_xml = ""
@num = 0
if @lenght != 0
ARGV.each_with_index do |a , x|
@num = @num + 1
b = ARGV[@num]
if ((a == "-f") && !(b.match "-") )
@factory_config_xml = b
x += 1
@num = @num + 1
elsif ((a == "-x") && !(b.match "-") )
@factory_config_xml = b
x += 1
@num = @num + 1
elsif ((a == "-p") && !(b.match "-") )
@factory_config_xml = b
x += 1
@num = @num + 1
end
end
end
puts @factory_config_xml
【问题讨论】:
-
为什么要到处使用实例变量?我的眼睛在流血……
-
你的意思是你想遍历你的数组在每次迭代中跳跃 2 吗?请具体说明您真正想要什么。
-
不是每次都是 2 个。我决定跳过运行时间。
标签: ruby arrays iteration command-line-arguments