以下は、配列内の項目の数が「有効」または「無効」であるかをチェックする Ruby の 2 行です。
valid = cr.select { |x| x.valid? }.count
invalid = cr.select { |x| !x.valid? }.count
2行目に構文エラーがある理由を誰でも見つけることができますか? デバッガーでこのコードをステップ実行しましたが、invalid = ...
行を実行しようとすると例外が発生します。2 つの行の唯一の違いはです。動作する!
ことを確認しました!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>'
変数cr
はAddressProcessingResult
オブジェクトの配列です。それらは次のようになります。
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