Skip to content

Instantly share code, notes, and snippets.

@mat-staszczyk
Last active February 25, 2016 08:24
Show Gist options
  • Save mat-staszczyk/317bbd857b6a259091c2 to your computer and use it in GitHub Desktop.
Save mat-staszczyk/317bbd857b6a259091c2 to your computer and use it in GitHub Desktop.
filename = ARGV[0]
class Analyzer
attr_reader :text
def initialize(filename)
@file = File.open(filename, "r+")
@text = generate_text
@file.close
end
def line_count
@file.readlines.length
end
def character_count
@text.clone.gsub(/\s+/, '').length
end
def total_length
@text.length
end
def words_count
@text.split.length
end
def sentence_count
@text.split(/\.|\?|\!/).length
end
def paragraph_count
@text.split(/\n\n/).length
end
def averages
puts "#{sentence_count / paragraph_count} sentences per paragraph (average)."
puts "#{words_count / sentence_count} words per sentence (average)."
end
private
def generate_text
string = ''
@file.each { |line| string << line }
string
end
end
test = Analyzer.new(filename)
test.averages
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment