Method: FileUtils.wc

Defined in:
lib/nano/fileutils/wc.rb

.wc(filename, option = 'all') ⇒ Object

With no arguments, returns a four element array consisting of the number of bytes, characters, words and lines in filename, respectively.

Valid options are bytes, characters (or just ‘chars’), words and lines.

# Return the number of words in 'myfile'
FileUtils.wc("myfile",'words')


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/nano/fileutils/wc.rb', line 19

def wc(filename,option='all')
  option.downcase!
  valid = %w/all bytes characters chars lines words/

  unless valid.include?(option)
      raise "Invalid option: '#{option}'"
  end

  n = 0
  if option == 'lines'
      IO.foreach(filename){ n += 1 }
      return n
  elsif option == 'bytes'
      File.open(filename){ |f|
        f.each_byte{ n += 1 }
      }
      return n
  elsif option == 'characters' || option == 'chars'
      File.open(filename){ |f|
        while f.getc
            n += 1
        end
      }
      return n
  elsif option == 'words'
      IO.foreach(filename){ |line|
        n += line.split.length
      }
      return n
  else
      bytes,chars,lines,words = 0,0,0,0
      IO.foreach(filename){ |line|
        lines += 1
        words += line.split.length
        chars += line.split('').length
      }
      File.open(filename){ |f|
        while f.getc
            bytes += 1
        end
      }
      return [bytes,chars,words,lines]
  end
end