Ruby/File Directory/Word counter
Материал из Wiki.crossplatform.ru
Содержание |
Counting Sentences and Paragraphs
puts "Test code! It works. Does it? Yes.".split(/\.|\?|!/).length
File word counter
lines = File.readlines("text.txt") line_count = lines.size text = lines.join puts "#{line_count} lines" word_count = text.split.length puts "#{word_count} words"
paragraph count
lines = File.readlines("text.txt") text = lines.join puts "#{line_count} lines" paragraph_count = text.split(/\n\n/).length puts "#{paragraph_count} paragraphs"
sentence count
lines = File.readlines("text.txt") line_count = lines.size text = lines.join puts "#{line_count} lines" sentence_count = text.split(/\.|\?|!/).length puts "#{sentence_count} sentences"