From: "Jesús Gabriel y Galán" Date: 2008-05-08T21:28:21+09:00 Subject: Re: Any reason why this code should not work - The Ruby Way, p270 On Thu, May 8, 2008 at 2:09 PM, 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 } I think this line should be self.each{|x| k[x] += 1} (notice the = sign, so the value gets assigned to the key). Otherwise you are never adding entries to the hash > k > end > end Hope this helps, Jesus.