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.
- data/README.rdoc +10 -0
- data/VERSION +1 -1
- data/bin/wc +1 -1
- data/lib/wc.rb +8 -9
- data/wc.gemspec +1 -1
- metadata +3 -3
data/README.rdoc
CHANGED
@@ -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.
|
1
|
+
0.30.0
|
data/bin/wc
CHANGED
data/lib/wc.rb
CHANGED
@@ -1,35 +1,34 @@
|
|
1
1
|
class Wc
|
2
|
-
|
2
|
+
attr_reader :filename, :occurrences
|
3
3
|
|
4
4
|
def initialize(filename)
|
5
5
|
@filename = filename
|
6
|
-
@
|
7
|
-
|
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
|
-
@
|
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
|
-
|
18
|
+
@sorted.to_json
|
20
19
|
end
|
21
20
|
|
22
21
|
private
|
23
22
|
|
24
23
|
def read()
|
25
|
-
|
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|
|
28
|
+
words.each { |w| occurrences[w] += 1 }
|
30
29
|
}
|
31
30
|
}
|
32
|
-
|
31
|
+
occurrences
|
33
32
|
end
|
34
33
|
|
35
34
|
end
|
data/wc.gemspec
CHANGED
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:
|
4
|
+
hash: 103
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 30
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.30.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Paolo Perego
|