Hi Everyone, Good Day, a=[ ''casual'',''sick'',''casual'',''sick'',''casual'',''sick'',''casual'',''sick'' ] I need to collect casual''s count and sick''s count like, Caual :4 Sick :4 Thank you, -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/VgwlFav-X6IJ. For more options, visit https://groups.google.com/groups/opt_out.
we can use a.count("casual") and viceversa. On Mon, Jan 21, 2013 at 12:03 PM, Maddy <ashokkumar-tks5Z7IV8F8RmelmmXo44Q@public.gmane.org> wrote:> Hi Everyone, > > Good Day, > > a=[ ''casual'',''sick'',''casual'',''sick'',''casual'',''sick'',''casual'',''sick'' ] > > I need to collect casual''s count and sick''s count like, > Caual :4 > Sick :4 > > Thank you, > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To view this discussion on the web visit > https://groups.google.com/d/msg/rubyonrails-talk/-/VgwlFav-X6IJ. > For more options, visit https://groups.google.com/groups/opt_out. > > >-- Mobile : +91 9035988755 Facebook : http://www.facebook.com/bala.kishore.21 linkedin : http://www.linkedin.com/profile/view?id=7562848 twitter : https://twitter.com/balakishorep -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
thanks :) On Mon, Jan 21, 2013 at 12:06 PM, bala kishore pulicherla < balumca21-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> a.count("casual")-- *"Attitude is a little thing that makes a big difference"* Thanks & Regards *Ashokkumar.Y* *ROR-Developer **email : ashokkumar-tks5Z7IV8F8RmelmmXo44Q@public.gmane.org* *Shriramits* * * -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
I tend to use a method like: module Enumerable # Returns a Hash keyed by the value of the block to the number times that # value was returned. If you have experience with the #group_by from # ActiveSupport, this would be like .group_by(&block).map{|k,a|[k,a.size]} # (except it is a Hash rather than an Array). def count_by counts = Hash.new(0) each {|e| counts[block_given? ? yield(e) : e] += 1} counts end end $ irb -r ./enumerable irb1.9.3> a=[ ''casual'',''sick'',''casual'',''sick'',''casual'',''sick'',''casual'',''sick'' ] #1.9.3 => ["casual", "sick", "casual", "sick", "casual", "sick", "casual", "sick"] irb1.9.3> a.count_by #1.9.3 => {"casual"=>4, "sick"=>4} This keeps you from iterating over the source multiple times, too. -Rob On Jan 21, 2013, at 1:37 AM, Ashokkumar Yuvarajan wrote:> thanks :) > On Mon, Jan 21, 2013 at 12:06 PM, bala kishore pulicherla <balumca21-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > a.count("casual") >On Mon, Jan 21, 2013 at 12:03 PM, Maddy <ashokkumar-tks5Z7IV8F8RmelmmXo44Q@public.gmane.org> wrote: Hi Everyone, Good Day, a=[ ''casual'',''sick'',''casual'',''sick'',''casual'',''sick'',''casual'',''sick'' ] I need to collect casual''s count and sick''s count like, Caual :4 Sick :4 Thank you,> -- > "Attitude is a little thing that makes a big difference" > > Thanks & Regards > Ashokkumar.Y > ROR-Developer > email : ashokkumar-tks5Z7IV8F8RmelmmXo44Q@public.gmane.org > Shriramits-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On Mon, Jan 21, 2013 at 11:21 AM, Rob Biedenharn <rob-GBZH0y1GwQfnZcttdmLDtcI/UQi/AW5J@public.gmane.org> wrote:> I tend to use a method like: > > module Enumerable > # Returns a Hash keyed by the value of the block to the number times that > # value was returned. If you have experience with the #group_by from > # ActiveSupport, this would be like .group_by(&block).map{|k,a|[k,a.size]} > # (except it is a Hash rather than an Array). > def count_by > counts = Hash.new(0) > each {|e| counts[block_given? ? yield(e) : e] += 1} > counts > end > endPlease do not teach people to monkey patch a class directly, if you plan to play dirty and reopen a class be nice enough to make your own module and then send(:include, MyModuleExt) so that people know where it is coming from easily, rather than leaving people in the dark as to what the hell is going on when they start looking for the method in the stdlib docs. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.