wc 0.22.0 → 0.30.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (6) hide show
  1. data/README.rdoc +10 -0
  2. data/VERSION +1 -1
  3. data/bin/wc +1 -1
  4. data/lib/wc.rb +8 -9
  5. data/wc.gemspec +1 -1
  6. metadata +3 -3
@@ -2,6 +2,16 @@
2
2
 
3
3
  wc gem is a very simple word counter. It takes a file in input and prints the words occurences.
4
4
 
5
+ == Usage
6
+
7
+ $ bin/wc filename
8
+
9
+ This is the very basic wc usage. It reads filename and it prints out a sorted
10
+ array for the word occurrences they have been found.
11
+
12
+ $ bin/wc --json filename
13
+ The same as the example above, except for the results that are printed in json notation.
14
+
5
15
  == Note on Patches/Pull Requests
6
16
 
7
17
  * Fork the project.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.22.0
1
+ 0.30.0
data/bin/wc CHANGED
@@ -43,7 +43,7 @@ if output == 'text'
43
43
  w.to_text
44
44
  end
45
45
  if output == 'json'
46
- w.to_json
46
+ p w.to_json
47
47
  end
48
48
  if output == 'xml'
49
49
  puts 'xml support is coming soon...'
data/lib/wc.rb CHANGED
@@ -1,35 +1,34 @@
1
1
  class Wc
2
- attr :filename
2
+ attr_reader :filename, :occurrences
3
3
 
4
4
  def initialize(filename)
5
5
  @filename = filename
6
- @hash = read
7
- p @hash.inspect
8
- Array(hash).sort { |one, two| -(one[1] <=> two[1]) }
6
+ @occurrences = read
7
+ @sorted = Array(occurrences).sort { |one, two| -(one[1] <=> two[1]) }
9
8
 
10
9
  end
11
10
 
12
11
  def to_text
13
- @hash.each { |elem|
12
+ @sorted.each { |elem|
14
13
  puts "\"#{elem[0]}\" has #{elem[1]} occurrences"
15
14
  }
16
15
  end
17
16
 
18
17
  def to_json
19
- puts @hash.to_json
18
+ @sorted.to_json
20
19
  end
21
20
 
22
21
  private
23
22
 
24
23
  def read()
25
- hash = Hash.new { |h, k| h[k] = 0 }
24
+ occurrences = Hash.new { |h, k| h[k] = 0 }
26
25
  File.open(@filename, "r") { |f|
27
26
  f.each_line { |line|
28
27
  words = line.split
29
- words.each { |w| hash[w] += 1 }
28
+ words.each { |w| occurrences[w] += 1 }
30
29
  }
31
30
  }
32
- hash
31
+ occurrences
33
32
  end
34
33
 
35
34
  end
data/wc.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{wc}
8
- s.version = "0.22.0"
8
+ s.version = "0.30.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Paolo Perego"]
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wc
3
3
  version: !ruby/object:Gem::Version
4
- hash: 71
4
+ hash: 103
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 22
8
+ - 30
9
9
  - 0
10
- version: 0.22.0
10
+ version: 0.30.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Paolo Perego