From: jzakiya Date: 2008-03-29T14:05:07+09:00 Subject: Re: numbers of a record in an array On Mar 28, 11:39 am, jzakiya wrote: > On Mar 27, 10:51 am, Paul Danese wrote: > > > > > If you install the facets gem you can use "frequency" > > > e.g. > > > >irb > > > irb:> require 'rubygems' > > => false > > irb:> require 'facets' > > => true > > irb:> x = %w[a b d e e rkeke ele e ee e e el d] > > => ["a", "b", "d", "e", "e", "rkeke", "ele", "e", "ee", "e", "e", "el", > > "d"] > > irb:> x.frequency > > => {"ee"=>1, "a"=>1, "b"=>1, "d"=>2, "e"=>5, "el"=>1, "ele"=>1, > > "rkeke"=>1} > > I installed facets 2.4.0 (the current latest) and tried to use the > 'frequency' method shown in this example, but in doesn't exist in this > version of facets. > > I did 'ri facets' and it only returned 'Enumerable#frequency' > This is using Linux (PCLOS) and Ruby 1.8.6-p114. > > What version of facets does the 'frequency' method exist in, because > it isn't in version 2.4.0? OK, I found what is going on with facets and the 'frequency' method. In facets 2.3.0 (2008/2/18) 'frequency' is a method under Enumerable, which got loaded by doing require 'rubygems' require 'facets' The current facets 2.4.0 (2008/3/24) 'frequency' has been moved to the 'probability.rb' file under the Enumerable module. So to load 'frequency' under facets 2.4.0 you must do require 'rubygems' require 'facets/enumerable/probability.rb' I would consider this a bug for breaking prior usage (by hiding the method in probability.rb). If you go to http://rubyforge.org/frs/?group_id=804&release_id=20470 and download the tar/zip files for each version you can see the changes. This is the facets source for 'frequency' def frequency #probs = Hash.new(0) #each do |e| # probs[e] += 1 #end #probs inject(Hash.new(0)){|h,v| h[v]+=1; h} end