2013-06-29 76 views
2

这里有红宝石的两行,检查有多少在阵列中的项目是‘有效’或‘无效’:现货红宝石语法错误:NameError:未定义局部变量或方法`'

valid = cr.select { |x| x.valid? }.count 
invalid = cr.select { |x| !x.valid? }.count 

任何人都可以发现为什么第二行有语法错误?我已经在调试器中加入了这段代码,并且在尝试执行invalid = ...行时引发异常。两条线的唯一区别是!,我已检查!true == false的作品。我很难过。


这是整个代码文件:

require "colored" 

require "address_kit/cli/interactive/command" 
require "address_kit/cli/interactive/shell" 

module AddressKit 
    module CLI 
    module Interactive 
     module Commands 

     class TableValidationStats < TableCommand 
      NAME = "table_validation_stats" 
      SUMMARY = "Displays statistics about the table address validation" 
      HELP_TEXT = <<-TEXT.chomp.reset_indentation 
      This command displays how many rows that has been validated, how 
      many of those rows were valid, invalid or auto-corrected. 
      TEXT 

      def execute(context, args) 
      super 

      shell = context.shell 
      results = context.vars[:address_processing_results] || [] 

      if results.length < 1 
       shell.puts_padded "No rows have been processed" 
       return 
      end 

      require "byebug"; byebug 

      cr = results.compact 
      total_processed = cr.count 
      failed_parsing = cr.select { |x| !x.parsed? }.count 
      valid = cr.select { |x| x.valid? }.count 
      invalid = cr.select { |x| !x.valid? }.count 
      corrected = cr.select { |x| x.corrected? }.count 

      shell.puts 
      shell.puts "Rows processed: #{total_processed.to_s.bold}" 
      shell.puts "Parse failures: #{failed_parsing.to_s.bold}" 
      shell.puts "Valid addresses: #{valid.to_s.bold}" 
      shell.puts "Invalid addresses: #{invalid.to_s.bold}" 
      shell.puts "Addresses auto-corrected: #{corrected.to_s.bold}" 
      shell.puts 
      end 
     end 

     Shell.register_command TableValidationStats 

     end 
    end 
    end 
end 

这是堆栈跟踪误差(忽略多余的文字,我的项目版画手工错误信息):

AN ERROR HAS OCCURRED! 

    The command you just ran produced an unexpected error. 
    Your shell session will not be lost, but please report 
    the following text to the maintainer of this project: 

    Exception: NameError 
    Message: undefined local variable or method ` ' for #<AddressKit::CLI::Interactive::Commands::TableValidationStats:0x00000001a6b840> 

    Stack trace: 
    /home/tomas/Dropbox/Kvantel/Address Kit/lib/address_kit/cli/interactive/commands/table_validation_stats.rb:37:in `block in execute' 
    /home/tomas/Dropbox/Kvantel/Address Kit/lib/address_kit/cli/interactive/commands/table_validation_stats.rb:37:in `select' 
    /home/tomas/Dropbox/Kvantel/Address Kit/lib/address_kit/cli/interactive/commands/table_validation_stats.rb:37:in `execute' 
    /home/tomas/Dropbox/Kvantel/Address Kit/lib/address_kit/cli/interactive/shell.rb:82:in `shell_iteration' 
    /home/tomas/Dropbox/Kvantel/Address Kit/lib/address_kit/cli/interactive/shell.rb:46:in `start' 
    bin/address_kit_shell:42:in `<main>' 

而且变量crAddressProcessingResult对象的数组。他们看起来是这样的:

module AddressKit 

    # This class represents the end result of the processing of an address, 
    # including normalization, parsing, validation and correction. 
    class AddressProcessingResult 
    attr_accessor :original_address, :parsed_address, :corrected_address, :note 
    attr_writer :parsed, :validated, :valid, :corrected 

    def initialize(original_address = nil) 
     @original_address = original_address 
     @parsed_address = nil 
     @corrected_address = nil 
     @note = "" 
     @parsed = false 
     @validated = false 
     @valid = false 
     @corrected = false 
    end 

    def parsed?; @parsed; end 
    def validated?; @validated; end 
    def valid?; @valid; end 
    def corrected?; @corrected; end 

    def readable_summary 
     if not parsed? 
     "Failed to parse address: #{@original_address}" 
     elsif valid? 
     "Address is valid: #{@parsed_address}" 
     elsif corrected? 
     "Address was auto-corrected: #{@corrected_address}: #{@note}" 
     else 
     "Address was invalid and could not be corrected: #{@corrected_address}" 
     end 
    end 
    end 

end 
+4

告诉我们错误..请问 –

+0

* cr *是什么? –

+0

@Priti:那简直太愚蠢了。我认为这是一个简单的,容易察觉的语法错误,所以我没有打算包含任何其他内容。我现在编辑了这个问题。 – Hubro

回答

9

您的代码中是否有Unicode空格字符(作为从其他地方复制粘贴的结果)? Ruby会将其解释为一个有效的变量名称!证明:

script = <<-EOF 
    #{"\u00A0"} = "foo" 
    puts #{"\u00A0"} 
EOF 

puts "The contents of the script are:" 
puts script 

puts "The output of the script is:" 
eval script 

输出:

The contents of the script are: 
      = "foo" 
    puts   
The output of the script is: 
foo 

我会使用一个十六进制编辑器或其他一些净化器在源代码检查Unicode字符。我可以产生相同的错误消息,如下所示:

> eval "puts #{"\u00A0"}" 
NameError: undefined local variable or method ` ' for main:Object 

您可以扫描非ASCII字符的文件,像这样:

def find_nonascii(file) 
    p_line = 1; p_char = 0 
    open(file, "r").each_char do |char| 
    if char.ord == 10 
     p_line += 1 
     p_char = 0 
    end 
    p_char += 1 
    puts "Found character #{char.ord} (#{char.inspect}) at line #{p_line}, character #{p_char}" if char.ord > 126 
    end 
end 

这会给你的第一个非ASCII字符的位置在脚本中。

您可以找到Unicode空格字符的列表here

+0

我更新了问题。 – Hubro

+0

并更新了我的答案。 –

+1

为什么你认为行的末尾将是'\ r'字符? ('13.chr ==“\ r”') –

相关问题