未定义的方法`include?' for nil:NilClass(NoMethodError)

时间:2014-06-14 23:41:55

标签: ruby-on-rails ruby include undefined

我有以下代码,并且正在使用ARGV。我会这样做,以便当用户离开空ARGV时,显示一些消息。

=begin

    TLDentifier developed by Arsh Leak. 2014.
    Commands:
        --display   :   Show all TLDs registered.
        --help      :   Show more informations.
        target.com

=end

   class String
    def green;          "\033[32m#{self}\033[0m" end
    def blue;           "\033[34m#{self}\033[0m" end
    def cyan;           "\033[36m#{self}\033[0m" end
    def bold;           "\033[1m#{self}\033[22m" end
   end


   system("clear")

   domains  =   {
    ".br"       =>  "Brazilian",
    ".com"      =>  "Comercial",
    ".aero"     =>  "Aircraft",
    ".biz"      =>  "Business",
    ".coop"     =>  "Cooperative",
    ".edu"      =>  "Educational",
    ".gov"      =>  "Government",
    ".info"     =>  "Information",
    ".int"      =>  "International organization",
   }

   def head()
    "TLDentifier".green.bold
   end
   puts head()
   name =   ARGV.first
   puts "[#{name}]".blue.bold
   domains.each do |domain, etn|
     if name.include? (domain)
       puts "["+domain.cyan+"]  is a "+etn+" Domain."
    elsif name  == "--help"
        system("clear")
        puts head()
        puts ""
        puts "Development.".cyan
        puts "Developed by "+"Arsh Leak. 2014."
        puts ""
        puts ""
        puts "GitHub.".cyan
        puts "github.com/4rsh"
        puts ""
        puts "Facebook.".cyan
        puts "facebook.com/doxnetwork"
        puts ""
        puts "Blog.".cyan
        puts "doxnetwork.com/"
        puts ""
        puts "Credits.".cyan
        puts "Prof. Eric Weinstein."
        puts ""
        puts "How to.".cyan
        puts "If you want to identify all TLDs, type:"
        puts "$ ruby ltdentifier.rb --display".green
        puts ""
        puts "If you want to identify a specific TLD, type:"
        puts "$ ruby ltdentifier.rb www.website.com/dir or www.website.com.".green
        puts ""
    elsif name == "--display"
        head()
        puts "Domain:".green+domain+" -"+" Entity: ".green+etn
      end
 end

并且,脚本会返回此错误:

tld.rb:302:在block in <main>': undefined method包含?' for nil:NilClass(NoMethodError)     来自tld.rb:301:each' from tld.rb:301:in'

1 个答案:

答案 0 :(得分:0)

如果没有传递参数,ARGV.first会返回nil,这不是字符串,因此不会响应include?nil的行为类似false,因此您可以通过输入

等代码来测试
unless name
  puts "Need at least one argument"
  exit
end
在您第一次使用name之前