创建具有范围的变量

时间:2016-03-31 18:30:31

标签: ruby range

我有一个尝试生成开放代理的程序,我将使用此程序作为测试工具,并且最近遇到了错误:

proxygen.rb:134:in `<=': comparison of Fixnum with nil failed (ArgumentError)
        from proxygen.rb:134:in `check_for_amount_of_proxies'
        from proxygen.rb:153:in `<main>'

发生的事情是我有变量@file_size和变量@amount@amount变量用于确定创建了多少可能的IP地址以及@file_size用于确定程序是否已找到所需数量的代理并将其保存到文件中。

来源:

#!/usr/local/bin/env ruby

require 'colored'
require 'timeout'
require 'net/ping'
require 'proxies-scanner'
require 'etc'

LOGIN = Etc.getlogin
PATH = Dir.pwd

def fatal(input)
  t = Time.now
  puts "\n[[#{t.hour}:#{t.min}:#{t.sec} FATAL]]#{input}".red.bold
end

def notice(input)
  t = Time.now
  puts "\n[#{t.hour}:#{t.min}:#{t.sec} NOTICE]#{input}\n".blue.bold
end

def err(input)
  t = Time.now
  puts "[#{t.hour}:#{t.min}:#{t.sec} ERROR]#{input}".red.bold
end

def info(input)
  t = Time.now
  puts "[#{t.hour}:#{t.min}:#{t.sec} INFO]#{input}".green.bold
end

def success(input)
  t= Time.now
  puts "[#{t.hour}:#{t.min}:#{t.sec} [SUCCESS]#{input}".green.bold
end

def warn(input)
  t = Time.now
  puts "[#{t.hour}:#{t.min}:#{t.sec} WARNING]#{input}".yellow.bold
end

def self.windows?
  return File.exist? "c:/WINDOWS" if RUBY_PLATFORM == 'java'
  RUBY_PLATFORM =~ /mingw32/ || RUBY_PLATFORM =~ /mswin32/
end

def self.linux?
  return File.exist? "/usr" if RUBY_PLATFORM == 'java'
  RUBY_PLATFORM =~ /linux/
end

def self.os
  return :linux if self.linux?
  return :windows if self.windows?
  nil
end

def check_os
  notice("Hello #{LOGIN}, it appears that #{os.capitalize} is your OS, path has been switched to #{PATH}")
end

def check_file
  if File.exist?("proxies.txt")
    File.truncate("proxies.txt", 0)
    notice("File exists in system, resuming process.")
  else 
    notice("File proxies.txt created successfully.")
    File.new("proxies.txt")
  end
end

def choose_proxy_amount
  print "Enter proxy amount: ".yellow.bold
  @amount = gets.chomp.to_i
  notice("Amount is measure in KB")
  case @amount
  when @amount >= 100
    @file_size = 0..2154
  when @amount <= 101
    @file_size = 2166..21618
  else

  end
end

def create_possibles
  notice("Attempting to ping generated IP addresses.")

  ports = %w(80 443 1935 2222 3128 3130 7808 8080 8081 8085 8089 
             8090 8102 8104 8106 8118 8119 8123 8888 8898 9000 
             9090 9797 9999 10000 10052 10053 10059 10088 12345 
             18000 18001 18008 37564 40080 55336 59998
            )

  @amount.times do
    @ip = Array.new(4){rand(256)}.join('.')
    begin
      Timeout::timeout(5) do 
        if Net::Ping::ICMP.new(@ip).ping?# == true
          success("Possible proxies created for IP: #{@ip}")
          File.open("proxies.txt", "a+") do |proxy|
            ports.each { |port| proxy.puts("#{@ip}:#{port}") }
          end
        else
          err("IP failed to ping: #{@ip}")
        end
      end
    rescue Timeout::Error
      warn("IP timed out: #{@ip}")
      next
    end
  end
end

def check_for_amount_of_proxies
  if File.size("proxies.txt") <= @file_size
    notice("Proxies created, attempting connection")
    system("proxies-scanner -f proxies.txt")
    warn("Truncating file: proxies.txt")
  else
    notice("File doesn't contain enough proxies, restarting IP finding proccess.")
    create_possibles
  end
end

begin
  check_file
  check_os
  choose_proxy_amount
  create_possibles
  check_for_amount_of_proxies
rescue RuntimeError => e
  fatal("This program requires Adminstration access to run, please switch to admin terminal.")
end

因此程序在尝试将文件大小与变量@file_size进行比较时失败。我的问题是有没有办法将这个变量创建到一个范围内?

1 个答案:

答案 0 :(得分:1)

你分配范围的方式很好。尝试将范围分配给IRB或Pry中的变量:

[3] (pry) main: 0> foo = 1..2
1..2
[4] (pry) main: 0> foo.class
Range < Object 

如果<= @file_size是范围:

,则无法使用@file_size
5 <= 1..6
ArgumentError: bad value for range

5 <= (1..6)
ArgumentError: comparison of Fixnum with Range failed

您可以改为使用===

(1..6) === 5
true

或:

@file_size === File.size("proxies.txt")

您无法撤消上述顺序。范围必须在左侧:

5 === (1..6)
false

有很多页面解释了原因。