Sha256: 5b979c002e54fbef0465dd391d5a541b8efddbb63f303e325fca716bfb51eb0a
Contents?: true
Size: 1.13 KB
Versions: 1
Compression:
Stored size: 1.13 KB
Contents
class Wc attr_reader :filename, :occurrences, :words def initialize(filename, words) @filename = filename @occurrences = read @sorted = Array(occurrences).sort { |one, two| -(one[1] <=> two[1]) } @words = words end def to_text if @words == -1 @sorted.each { |elem| puts "\"#{elem[0]}\" has #{elem[1]} occurrences" } else @sorted[0..@words-1].each { |elem| puts "\"#{elem[0]}\" has #{elem[1]} occurrences" } end end def to_json if @words == -1 return @sorted.to_json else return @sorted[0..@words-1].to_json end end def to_cloud if @words == -1 cloud_items = @sorted else cloud_item = @sorted[0..@words-1] end ret = "<dl>" cloud_item.each { |elem| ret+="<dt>" + elem[0] +"</dt>" } ret += "</dl>" ret end private def read() occurrences = Hash.new { |h, k| h[k] = 0 } File.open(@filename, "r") { |f| f.each_line { |line| words = line.split words.each { |w| occurrences[w] += 1 } } } occurrences end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
wc-0.90.0 | lib/wc.rb |