Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. class Mapper
  4.  
  5. # initialize a mapper with raw data.
  6. def initialize(line)
  7. # chomp will remove endline characters
  8. # split will split the line for every tab character \t
  9. # strip will remove whitespaces at begining and end of every words
  10. @data = line.chomp.split("\t").map(&:strip)
  11. end
  12.  
  13. # this "switch" will determine if we are reading a GameLog or a UserLog line
  14. # in our example, it is sufficient to look whether @data has 2, or 3 values
  15. # for more complex cases, I'm sure you'll always find something ;)
  16. def log_type
  17. @log_type ||= if @data.size == 2
  18. :game_log
  19. else
  20. :player_log
  21. end
  22. end
  23.  
  24. def game_log_output
  25. game_id = @data[0]
  26. game_type = @data[1]
  27.  
  28. [game_id, log_type, game_type].join("\t")
  29. end
  30.  
  31. def player_log_output
  32. player_id = @data[0]
  33. score = @data[1]
  34. game_id = @data[2]
  35.  
  36. [game_id, log_type, player_id, score].join("\t")
  37. end
  38.  
  39. # the mapper result
  40. def output
  41. return game_log_output if log_type == :game_log
  42. return player_log_output
  43. end
  44.  
  45. # the Map! class method
  46. def self.map!(line)
  47. puts Mapper.new(line).output
  48. end
  49.  
  50. end
  51.  
  52. ARGF.each do |line|
  53. Mapper.map!(line) unless line.chomp.empty? # map every non-empty line with our mapper
  54. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement