导致"已经初始化的常量"警告的原因是什么?

Acc*_*Acc 3 ruby search dir

我的代码出了什么问题?正在FileNameArray被重用?

f.rb:17:警告:已经初始化了常量FileNameArray

number = 0
while number < 99
  number = number + 1
  if number <= 9
    numbers = "000" + number.to_s
  elsif
    numbers = "00" + number.to_s
  end
  files = Dir.glob("/home/product/" + numbers + "/*/*.txt")
    files.each do |file_name|
    File.open(file_name,"r:utf-8").each do | txt |
      if txt =~ /http:\/\//
        if txt =~ /static.abc.com/ or txt =~ /static0[1-9].abc.com/
        elsif
        $find = txt
        FileNameArray = file_name.split('/')
        f = File.open("error.txt", 'a+')
        f.puts FileNameArray[8], txt , "\n"
        f.close
        end
      end
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

nkm*_*nkm 5

你可能是一个红宝石初学者,我试图用ruby方式重写相同的代码......

(1..99).each do |number|
  Dir.glob("/home/product/" + ("%04d" % numbers) + "/*/*.txt").each do |file_name|
    File.open(file_name,"r:utf-8").each do | txt |
      next unless txt =~ /http:\/\//
      next if txt =~ /static.abc.com/ || txt =~ /static0[1-9].abc.com/        

      $find = txt
      file_name_array = file_name.split('/')
      f = File.open("error.txt", 'a+')
      f.puts file_name_array[8], txt , "\n"
      f.close      
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

注意事项,

  1. 在ruby中,如果使用带有$符号前缀的变量,则将其视为a global variable.因此$find,只有在需要时才使用.
  2. 在ruby中constant variable开始capital letter,通常我们应该改变一个常量值.这可能导致程序中的错误.
  3. (1..99)是一个用于创建Range类实例的文字,它返回1到99之间的值