From: Rolando Abarca Date: 2008-05-08T21:26:45+09:00 Subject: Re: Any reason why this code should not work - The Ruby Way, p270 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.