From: Victor Reyes Date: 2008-05-08T21:43:37+09:00 Subject: Re: Any reason why this code should not work - The Ruby Way, p270 ------=_Part_1028_24199214.1210250614581 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Thank you Rolando. It was my silly typo! On Thu, May 8, 2008 at 8:26 AM, Rolando Abarca wrote: > On May 8, 2008, at 8:09 AM, Victor Reyes wrote: > > I lifted the following code from The Ruby way and tried to use it. >> It does not output anything. >> Do anyone knows why? >> >> Thank you >> >> Victor >> >> ############################## >> # This code adds a count method to the Array class >> # This code was lifted from: >> # The Ruby Way >> # Second Edition >> # By: Hal Fulton >> # Page: 270 >> ############################## >> class Array >> def count >> k=Hash.new(0) >> self.each{|x| k[x]+1 } >> k >> end >> end >> > > I think it should read: > > def count > k = Hash.new(0) > self.each { |x| k[x] += 1 } > k > end > > The problem is that inside the 'each' loop you're not asigning any value. > regards, > -- > Rolando Abarca M. > > > ------=_Part_1028_24199214.1210250614581--