Skip to content

Instantly share code, notes, and snippets.

@Canx
Created February 29, 2012 22:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Canx/1945149 to your computer and use it in GitHub Desktop.
Save Canx/1945149 to your computer and use it in GitHub Desktop.
kata 4: data munging (part 3)
class TabFile < File
def initialize(file, regexp)
super(file, "r")
@regexp = regexp
end
def tab_data
data = Array.new
self.each { |line|
line_match = @regexp.match(line)
if !line_match.nil? then
data << line_match.captures.collect { |v| v.match(/^\d+$/)? v.to_i : v }
end
}
return data
end
end
puts TabFile.new("weather.dat", /^\s+([0-9]+)\s+([0-9]+)[*]?\s+([0-9]+)[*]?/).tab_data.min {|a,b| a[1]-a[2] <=> b[1]-b[2] }[0]
puts TabFile.new("football.dat", /^\s+[0-9]+[.]\s([a-zA-Z_]+)\s+[0-9]+\s+[0-9]+\s+[0-9]+\s+[0-9]+\s+([0-9]+)\s+-\s+([0-9]+)/).tab_data.min { |a,b| (a[1]-a[2]).abs <=> (b[1]-b[2]).abs }[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment