On Sep 18, 4:17 pm, Nick Bo <bornema... / nku.edu> wrote: > irb > irb(main):001:0> string ="cat cat fox" > => "cat cat fox" > irb(main):002:0> stringArray = string.split(" ") > => ["cat", "cat", "fox"] > irb(main):003:0> string.count(stringArray[0]) > => 6 > > obviously what i want it to do is to count how many instances of cat > there are in a string by using an array. Reason for this is i want to > be able to push the value of the count into a new array and make a hash > using (word, count) and then be able to print the word and count and > then do a method which will print the word and some kind of delimeter > that will express how many times cat showed up so... > > cat| ## > > or something like that but first thing is first why wont it let me count > how many instances of cat were in the string using an array? and why > does it tell me 6? count = 0 ==>0 "cat cat fox".scan( "cat" ){ count += 1 } ==>"cat cat fox" count ==>2 "cat cat fox".scan( "cat" ).size ==>2