From: "David A. Black" Date: 2007-04-19T23:25:06+09:00 Subject: Re: How to count a word in a line Hi -- On 4/19/07, Yusuf Celik wrote: > Sorry made a mistake > line=gets.chomp > cnt = 0 > line.split.each { > |v| > if v == 'Hi' then > cnt +=1 > end > } > puts "#{cnt}" You could do: line.scan(/\bHi\b/).size (or \s instead of \b if you want it to be spaces instead of word boundaries [which can also be hyphens and other punctuation]). David -- Upcoming Rails training by Ruby Power and Light: Four-day Intro to Intermediate May 8-11, 2007 Edison, NJ http://www.rubypal.com/events/05082007