Ruby simple Array/Iterator? -


i have story written in .txt file , have count number of letters , words divide both in order find average. unable find total number of letters or total number of words. when run program, ruby shows me list of numbers think number of letters per word. i'm looking total letters i'm not sure how make ruby add everything. here code. iterator count total number of words ".count" ?

myfile = file.new("story.txt", "r") contents = myfile.read wordlist = contents.split wordlist.each |length| puts length.size.to_s end 

also iterator count total number of words ".count" ?

no. count() string method, can read in string docs.

on other hand, each() iterator.

you can length or size of string, described in string docs.

so, this:

words = %w[ cat ]  #shortcut ["a", "to", "cat"]                         #saves having type quotes. letter_count = 0 word_count = 0  words.each |word|   word_count += 1   letter_count += word.length end  puts "the count of words is: #{word_count}" puts "the total number of letters is: #{letter_count}"  --output:-- count of words is: 3 total number of letters is: 6 

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -