新手问题 帮忙看一下代码哪里有问题

lrbnew · 2015年07月18日 · 最后由 lrbnew 回复于 2015年07月19日 · 1675 次阅读

附代码如下

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"}

def split_dest_ip(nat)
  ips = nat['d_ip'].split('|').reject(&:empty?)
  ips.map{ |ip| new_nat = nat.dup; new_nat['d_ip'] = ip; new_nat }
end

未测!

#1 楼 @gihnius 可以了,多谢。 看来还有许多要学习啊

#1 楼 @gihnius 问题出在哪呢?没想明白

nat 被重复付值了,

#3 楼 @lrbnew 你一直把同一个 nat 的值放进数组,

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 你可以通过 object_id 查看:

p nat
l = split_dest_ip nat
l[0].object_id
l[1].object_id
l[2].object_id
p nat

你可以改为这样试试:

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.dup
    binding.pry
  end
  return l
end
需要 登录 后方可回复, 如果你还没有账号请 注册新账号