发送多个参数到哈希

时间:2014-04-17 08:40:59

标签: ruby ruby-on-rails-3

HY 我解析文件,找到一些像uid,dbname和url这样的参数。 我尝试做的是获取散列的所有值,以便在csv文件中发送它

当我解析我的哈希值(res_licence)时,我只有3个这样的参数

uid,uid'] ='adfs'; url,url'] ='https://mydom.sandboxbk.net'; dbname,

我无法找到他为什么覆盖:(

res_licence = Hash.new

array = Dir.glob('configuration_*')
array.each do |f|
       file = File.open(f ,"r")
      lines = file.readlines

  lines.each do |line|

  if line.include? 'uid'
     uid = line.match(/(?=uid)(.*)/)
     puts ""
     puts "==>#{uid} "
     #res_licence = { uid: uid}
  end 


 if line.include? 'url'
     url = line.match(/(?=url)(.*)/)
     puts "==>#{url} "
       #res_licence = {  url: url}
  end 


 if line.include? 'dbname'
     dbname = line.match(/(?=dbname)(.*)/)
     puts "=>#{dbname} "

  end 
    res_licence = {uid: uid, url: url, dbname: dbname}
   end

 end

2 个答案:

答案 0 :(得分:0)

我的猜测是变量在你分配它们的块中是本地的。在脚本的开头添加:

uid = nil
url = nil
dbname = nil

答案 1 :(得分:0)

我找到了解决问题的方法,这次我没有uid => “xxx”,url => “xxx”

configs = {}
Dir.glob('configuration_*').each do |filename|
 configs[filename]= Hash.new
  File.foreach(filename) do |line|


         if line.include? 'uid'
              next unless line.match(/(?=uid).*?[\'\"].*?[\'\"](.*?)[\'\"](.*?)[\;\"]/)
                field, value = line.split("=",2);
             ( configs[filename] = []  <<  value.strip.gsub("';", "").gsub("'",""))
               #( configs[filename][:uid] ||= [] ) <<  value.strip.gsub("';", "").gsub("'","") 
         end
   CSV.open("/tmp/data.csv", "wb", Options = {quote_char: "\0" }) {|csv| configs.each {|input| csv << input }}

现在我执行脚本时遇到了输出

的最后一个问题
configuration_groupe-poult.mydomain.net.php,["group", "https://groupe-poult.mydomain.net", "group"]
configuration_kb-in.mydomain.net.php,["kbin", "https://kb-in.mydomain.net", "kauf"]

我如何删除钩子'[]和引用“?