Sha256: 6fea5a0e1a310c09fcfb48ce82e9d8f59f518e6a2843a302757bdc55f06dee1e

Contents?: true

Size: 576 Bytes

Versions: 1

Compression:

Stored size: 576 Bytes

Contents

class Wc 
  attr :filename
  
  def initialize(filename)
    @filename = filename
    @hash = read
    Array(hash).sort { |one, two| -(one <=> two) }
    
  end
  
  def to_text
      @hash.each { |elem|
        puts "\"#{elem[0]}\" has #{elem[1]} occurrences"
      }
  end
  
  def to_json
    puts @hash.to_json
  end
  
  private
  
  def read() 
    hash = Hash.new { |h, k| h[k] = 0 }
    File.open(@filename, "r") { |f|
          f.each_line { |line|
            words = line.split
            words.each { |w| hash[w] += 1 }
          }
        }
    hash
  end
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wc-0.21.0 lib/wc.rb