Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- class Mapper
- # initialize a mapper with raw data.
- def initialize(line)
- # chomp will remove endline characters
- # split will split the line for every tab character \t
- # strip will remove whitespaces at begining and end of every words
- @data = line.chomp.split("\t").map(&:strip)
- end
- # this "switch" will determine if we are reading a GameLog or a UserLog line
- # in our example, it is sufficient to look whether @data has 2, or 3 values
- # for more complex cases, I'm sure you'll always find something ;)
- def log_type
- @log_type ||= if @data.size == 2
- :game_log
- else
- :player_log
- end
- end
- def game_log_output
- game_id = @data[0]
- game_type = @data[1]
- [game_id, log_type, game_type].join("\t")
- end
- def player_log_output
- player_id = @data[0]
- score = @data[1]
- game_id = @data[2]
- [game_id, log_type, player_id, score].join("\t")
- end
- # the mapper result
- def output
- return game_log_output if log_type == :game_log
- return player_log_output
- end
- # the Map! class method
- def self.map!(line)
- puts Mapper.new(line).output
- end
- end
- ARGF.each do |line|
- Mapper.map!(line) unless line.chomp.empty? # map every non-empty line with our mapper
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement