Module: Cwc
- Defined in:
- lib/cwc.rb
Overview
lib/cwc.rb
Constant Summary collapse
- VERSION =
"1.1.0"
Class Method Summary collapse
- .countBytes(file) ⇒ Object
- .countChars(file) ⇒ Object
- .countLines(file) ⇒ Object
- .countWords(file) ⇒ Object
- .main ⇒ Object
Class Method Details
.countBytes(file) ⇒ Object
37 38 39 |
# File 'lib/cwc.rb', line 37 def countBytes(file) return File.size(file) end |
.countChars(file) ⇒ Object
46 47 48 |
# File 'lib/cwc.rb', line 46 def countChars(file) return File.read(file).size end |
.countLines(file) ⇒ Object
40 41 42 |
# File 'lib/cwc.rb', line 40 def countLines(file) return File.readlines(file).size end |
.countWords(file) ⇒ Object
43 44 45 |
# File 'lib/cwc.rb', line 43 def countWords(file) return File.read(file).split.size end |
.main ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/cwc.rb', line 50 def main o = "" unless ARGV.empty? file = ARGV[0] else file = "" end if not STDIN bytes = countBytes(file) words = countWords(file) lines = countLines(file) else input = STDIN.read bytes = input.size words = input.split.size lines = input.split("\n").size end if $options.key?(:bytes) number = bytes end if $options.key?(:words) number = words end if $options.key?(:lines) number = lines end if $options.key?(:chars) number = countChars(file) end if $options.length < 1 o = "\t%d\t%d\t%d\t%s" % [lines, words, bytes, file] else o = "\t%d\t%s" % [number, file] end return o end |