Read the files in the variables, using Dir and arrays

advertisements

For an assignment, I'm using the Dir.glob method to read a series of famous speech files, and then perform some basic speech analytics on each one (number of words, number of sentences, etc). I'm able to read the files, but have not figured out how to read each file into a variable, so that I may operate on the variables later.

What I've got is:

Dir.glob('/students/~pathname/public_html/speeches/*.txt').each do |speech|
    #code to process the speech.
    lines = File.readlines(speech)
    puts lines
end

This prints all the speeches out onto the page as one huge block of text. Can anyone offer some ideas as to why?

What I'd like to do, within that code block, is to read each file into a variable, and then perform operations on each variable such as:

Dir.glob('/students/~pathname/public_html/speeches/*.txt').each do |speech|
    #code to process the speech.
    lines = File.readlines(speech)
    text = lines.join
    line_count = lines.size
    sentence_count = text.split(/\.|\?|!/).length
    paragraph_count = text.split(/\n\n/).length
    puts "#{line_count} lines"
    puts "#{sentence_count} sentences"
    puts "#{paragraph_count} paragraphs"
end

Any advice or insight would be hugely appreciated! Thanks!


Regarding your first question:

readLines converts the file into an array of Strings and what you then see is the behaviour of puts with an array of Strings as the argument.

Try puts lines.inspect if you would rather see the data as an array.

Also: Have a look at the Ruby console irb in case you have not done so already. It is very useful for trying out the kinds of things you are asking about.