如何在Ruby GetoptLong对象中迭代两次?
请看这个名为foo.rb的简单代码:如何在Ruby GetoptLong对象中迭代两次?,ruby,command-line-arguments,Ruby,Command Line Arguments,请看这个名为foo.rb的简单代码: require( 'getoptlong' ) opts = GetoptLong.new( [ '--ies', GetoptLong::OPTIONAL_ARGUMENT ] ) opts.each do | opt, arg | if( opt == '--ies' ) puts arg end end opts.each do | opt, arg | if( opt == '--ies' ) puts arg en
require( 'getoptlong' )
opts = GetoptLong.new( [ '--ies', GetoptLong::OPTIONAL_ARGUMENT ] )
opts.each do | opt, arg |
if( opt == '--ies' )
puts arg
end
end
opts.each do | opt, arg |
if( opt == '--ies' )
puts arg
end
end
我希望如果我在LINUX中输入:
foo.rb --ies=bar
我会得到:
bar
bar
但是,我只有一行:
bar
为什么我不能在GetoptLong实例中迭代两次?
谢谢,实现不允许这样做
GetoptLong#每个
在循环中调用,直到返回nil
,即“处理完成”。一旦完成,它总是返回nil
,因此无法再次处理
您可以创建一个数组:
opts_array = []
opts.each { |o, a| opts_array << [o, a] }
opts_array.each do |opt, arg|
# ...
end
opts_array.each do |opt, arg|
# ...
end
opts_数组=[]
opts.each{| o,我推荐一个| opts|u数组。它更易于使用。