运行我下面的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脚本,你应该将该逻辑移出课程之外。 –