運行我下面的Ruby腳本時,我收到以下錯誤:紅寶石,沒有方法錯誤
s3parse.rb:12:in `block in <class:AccountLog>': undefined method `extract_account_id' for AccountLog:Class (NoMethodError)
我不認爲它應該是一個類的方法,是有一個原因它不是以我的方法考慮?
class AccountLog
attr_accessor :bytes, :account_id, :date
def extract_account_id(line)
line.match(%r{accounts/(\d+)}).captures.join.to_i
end
s3log = File.open('vidcoder.txt').each do |line|
account_log = AccountLog.new
account_log.date = line.match(%r{\[[^:]*}).to_s.delete"[" #need to finish this regex to make it work
account_log.account_id = extract_account_id(line)
account_log.bytes = line.match(%r{^.*\s+HTTP.*\s+-\s+(\d+)\s+}).captures.join.to_i
puts "\n"
puts "The api request on #{account_log.date} was fromm account number #{account_log.account_id} and the bytes were #{account_log.bytes}"
end
end
你可以發佈s3parse.rb或至少使用AccountLog類的相關部分嗎? – jboursiquot
嗨,這是整個類和文件,我只是從命令行運行該文件,我的目標是將正則表達式重構爲單獨的方法,以使其更好地讀取 – BC00
您正在嘗試使用s3log變量?如果這是一個ruby腳本,你應該將該邏輯移出課程之外。 –