Skip to content

Instantly share code, notes, and snippets.

Created January 17, 2014 22:20
Show Gist options
  • Save anonymous/b85b0374e8e0f6607421 to your computer and use it in GitHub Desktop.
Save anonymous/b85b0374e8e0f6607421 to your computer and use it in GitHub Desktop.
`block in read_file': undefined method `center' for nil:NilClass (NoMethodError) from version_2.rb:11:in `each_line' from version_2.rb:11:in `read_file' from version_2.rb:7:in `initialize' from version_2.rb:69:in `new' from version_2.rb:69:in `<main>'
class UtilitiesStats
attr_accessor :all_cust
def initialize data
self.all_cust = []
read_file data
end
def read_file data
File.open( data , "r").each_line do |line|
data = line.split("|")
cust_id, elect_or_gas, disconnect_doc, move_in_date, move_out_date, bill_year, bill_month, span_days, meter_read_date,
meter_read_type, consumption, exception_code = data
if cust_exist = all_cust.detect {|k| k[cust_id]}
next if cust_exist['CustID']
index = all_cust.index(cust_exist)
all_cust[index][cust_id] << {
elect_or_gas: elect_or_gas, disconnect_doc: disconnect_doc, move_in_date: move_in_date,
move_out_date: move_out_date, bill_year: bill_year, bill_month: bill_month,
span_days: span_days, meter_read_date: meter_read_date, meter_read_type: meter_read_type, consumption: consumption,
exception_code: exception_code
}
else
all_cust << { cust_id => [{
elect_or_gas: elect_or_gas, disconnect_doc: disconnect_doc, move_in_date: move_in_date,
move_out_date: move_out_date, bill_year: bill_year, bill_month: bill_month,
span_days: span_days, meter_read_date: meter_read_date, meter_read_type: meter_read_type, consumption: consumption,
exception_code: exception_code
}]
}
end
c = 15 #nbr of characters with string in center for prettier output
print "#{cust_id.center(c)}| #{elect_or_gas.center(c)}| #{disconnect_doc.center(c)}| #{move_in_date.center(c)}| #{move_out_date.center(c)}| #{bill_year.center(c)}| #{bill_month.center(c)}| #{span_days.center(c)}| #{meter_read_date.center(c)}| #{meter_read_type.center(c)}| #{consumption.center(c)}| #{exception_code.center(c)}\n"
end
end
u = UtilitiesStats.new "./pulse_data.txt"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment