换句话说,我需要 Ruby 从附加的日志文件中读取并报告具有关键字 MAY_DAY 的日期和时间。我能够打印出所有信息,但我对如何打印出特定条目一无所知。
我是一个超级菜鸟,发现红宝石非常难以理解。我感谢所有帮助和尊重的批评。谢谢
测试.txt
Oct 15 12:54:01 WHERE IS THE LOVIN MAY_DAY
Oct 16 23:15:44 WHAT THE HECK CAN I DO ABOUT IT HUMP_DAY
Oct 16 14:16:09 I LOVE MY BABY GIRL MAY_DAY
Oct 16 08:25:18 CAN WAIT UNTIL MY BABY RECOVERS CRYSTAL_WIFE
Oct 18 17:48:38 I HOPE HE STOP MESSING WITH THESE FOOLISH CHILDREN TONY_SMITH
Oct 19 05:17:58 GAME TIME GO HEAD AND GET ME MAY_DAY
Oct 20 10:23:33 GAMESTOP IS WHERE ITS AT GAME_DAY
Oct 21 03:54:27 WHAT IS GOING ON WITH MY LUNCH HUNGRY_MAN
重启监视器.rb
class RestartMonitor
counter = 1
begin
file = File.new("test.txt", "r")
while (line = file.gets)
puts "#{counter}: #{line}"
counter = counter + 1
end
end
当我运行该文件时,我得到以下结果:
Oct 15 12:54:01 WHERE IS THE LOVIN MAY_DAY
Oct 16 23:15:44 WHAT THE HECK CAN I DO ABOUT IT HUMP_DAY
Oct 16 14:16:09 I LOVE MY BABY GIRL MAY_DAY
Oct 16 08:25:18 CAN WAIT UNTIL MY BABY RECOVERS CRYSTAL_WIFE
Oct 18 17:48:38 I HOPE HE STOP MESSING WITH THESE FOOLISH CHILDREN TONY_SMITH
Oct 19 05:17:58 GAME TIME GO HEAD AND GET ME MAY_DAY
Oct 20 10:23:33 GAMESTOP IS WHERE ITS AT GAME_DAY
Oct 21 03:54:27 WHAT IS GOING ON WITH MY LUNCH HUNGRY_MAN
当我运行代码时,我希望它只显示具有关键世界 MAY_DAY 的日期和时间。所以输出应该是:
Oct 15 12:54:01
Oct 16 14:16:09
Oct 19 05:17:58