附代码如下
require 'pry'
nat = {"nat_num"=>"1", "nat_orig_s"=>"OA-R-U4-01", "nat_orig_d"=>"AR-GROUP", "nat_orig_port"=>"Any", "nat_trans_s"=>"IFAR", "nat_trans_d"=>"Any", "nat_trans_port"=>"Any", "nat_type"=>"source_nat", "nat_valid_ip"=>"10.235.151.185", "s_ip"=>"10.237.195.225", "d_ip"=>"|10.237.190.32|10.237.190.33|10.237.190.57"}
def split_dest_ip(nat)
l = []
ips = nat["d_ip"].split("|")[1..-1]
ips.each do |ip|
nat.delete("d_ip")
nat["d_ip"] = ip
l << nat
binding.pry
end
return l
end
puts split_dest_ip nat
本意想按照"d_ip"元素来拆分 nat 变量,使其变成: {"nat_num"=>"1", "nat_orig_s"=>"OA-R-U4-01", "nat_orig_d"=>"AR-GROUP", "nat_orig_port"=>"Any", "nat_trans_s"=>"IFAR", "nat_trans_d"=>"Any", "nat_trans_port"=>"Any", "nat_type"=>"source_nat", "nat_valid_ip"=>"10.235.151.185", "s_ip"=>"10.237.195.225", "d_ip"=>"10.237.190.32"} {"nat_num"=>"1", "nat_orig_s"=>"OA-R-U4-01", "nat_orig_d"=>"AR-GROUP", "nat_orig_port"=>"Any", "nat_trans_s"=>"IFAR", "nat_trans_d"=>"Any", "nat_trans_port"=>"Any", "nat_type"=>"source_nat", "nat_valid_ip"=>"10.235.151.185", "s_ip"=>"10.237.195.225", "d_ip"=>"10.237.190.33"} {"nat_num"=>"1", "nat_orig_s"=>"OA-R-U4-01", "nat_orig_d"=>"AR-GROUP", "nat_orig_port"=>"Any", "nat_trans_s"=>"IFAR", "nat_trans_d"=>"Any", "nat_trans_port"=>"Any", "nat_type"=>"source_nat", "nat_valid_ip"=>"10.235.151.185", "s_ip"=>"10.237.195.225", "d_ip"=>"|10.237.190.57"}
结果悲剧了,成了 {"nat_num"=>"1", "nat_orig_s"=>"OA-R-U4-01", "nat_orig_d"=>"AR-GROUP", "nat_orig_port"=>"Any", "nat_trans_s"=>"IFAR", "nat_trans_d"=>"Any", "nat_trans_port"=>"Any", "nat_type"=>"source_nat", "nat_valid_ip"=>"10.235.151.185", "s_ip"=>"10.237.195.225", "d_ip"=>"|10.237.190.57"} {"nat_num"=>"1", "nat_orig_s"=>"OA-R-U4-01", "nat_orig_d"=>"AR-GROUP", "nat_orig_port"=>"Any", "nat_trans_s"=>"IFAR", "nat_trans_d"=>"Any", "nat_trans_port"=>"Any", "nat_type"=>"source_nat", "nat_valid_ip"=>"10.235.151.185", "s_ip"=>"10.237.195.225", "d_ip"=>"|10.237.190.57"} {"nat_num"=>"1", "nat_orig_s"=>"OA-R-U4-01", "nat_orig_d"=>"AR-GROUP", "nat_orig_port"=>"Any", "nat_trans_s"=>"IFAR", "nat_trans_d"=>"Any", "nat_trans_port"=>"Any", "nat_type"=>"source_nat", "nat_valid_ip"=>"10.235.151.185", "s_ip"=>"10.237.195.225", "d_ip"=>"|10.237.190.57"}