From: Rob Biedenharn on 20 Jul 2010 11:05 On Jul 20, 2010, at 10:58 AM, Vitaliy Yanchuk wrote: > Jean-Julien Fleck wrote: >> 2010/7/20 Vitaliy Yanchuk <fuksito(a)gmail.com>: >>> Jean-Julien Fleck, thanks. >>> Maybe have an idea of a one-line version? :) >> >> Well: quite the same using inject: >> >> arr.inject(Hash.new(0)) {|h,e| h[e]+= 1; h} >> >> It all depends on what you call 'one-line' :o) >> >> Cheers, > > Wow, Inject is cool method, thanks ! > In my mind one-line is that dows not have semi-coloms or new lines of > course > -- Soo... that has a semi-colon and isn't then a one-liner?? ;-) -Rob Rob Biedenharn Rob(a)AgileConsultingLLC.com http://AgileConsultingLLC.com/ rab(a)GaslightSoftware.com http://GaslightSoftware.com/
From: Vitaliy Yanchuk on 20 Jul 2010 11:08 Rob Biedenharn wrote: > Soo... that has a semi-colon and isn't then a one-liner?? ;-) Yeah, not perfectly one-liner. But it is without dot-chain breaking, not bad too :) -- Posted via http://www.ruby-forum.com/.
From: Joseph E. Savard on 20 Jul 2010 11:16 h = Hash.new(0) ["a", "b", "a", "c", "c", "b", "b"].each {|s| h[s] += 1}; > From: Vitaliy Yanchuk <fuksito(a)gmail.com> > Reply-To: <ruby-talk(a)ruby-lang.org> > Newsgroups: comp.lang.ruby > Date: Tue, 20 Jul 2010 22:47:27 +0900 > To: ruby-talk ML <ruby-talk(a)ruby-lang.org> > Subject: Count occurences of vars in array > > Hello, everyone. > > Would be grateful if someone can tell, how can I do shortly (mb with one > method) from such example array > > ["a", "b", "a", "c", "c", "b", "b"] > > The result > a => 2 > b => 3 > c => 2 > > So, to count number of occurances > -- > Posted via http://www.ruby-forum.com/. >
From: Joseph E. Savard on 20 Jul 2010 11:19 Nice! > arr.inject(Hash.new(0)) {|h,e| h[e]+= 1; h} > -- > JJ Fleck > PCSI1 Lycée Kléber >
From: Jean-Julien Fleck on 20 Jul 2010 11:21 2010/7/20 Vitaliy Yanchuk <fuksito(a)gmail.com>: > Rob Biedenharn wrote: >> Soo... that has a semi-colon and isn't then a one-liner?? ;-) > > Yeah, not perfectly one-liner. But it is without dot-chain breaking, not > bad too :) That would be a real one-liner if I could recall how to convert elegantly an array of size2 arrays into an hash: arr.group_by {|o| o}.collect{|k,v| [k,v.size]} Cheers, -- JJ Fleck PCSI1 Lycée Kléber
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 Prev: undefined method `call' for nil:NilClass (NoMethodError) Next: Ruby Shoes tray icon? |