Todd Benson
9/19/2008 12:43:00 AM
On Thu, Sep 18, 2008 at 4:17 PM, Nick Bo <bornemann1@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)
a = "duck","duck","goose"
a.inject(Hash.new(0)) {|h, e| h[e] += 1; h}
=> {"goose"=>1, "duck"=>2}
hth a little,
Todd