Ask a Question related to Ruby, Design and Development.
-
Ian Macdonald #1
Re: Ruby Code Snippet: Array.count
On Sat 05 Jul 2003 at 08:01:38 +0900, Stefan Arentz wrote:
This is already available with Enumerable#find_all:>
> Maybe this functionality is already somewhere in Ruby, but I could not
> find it. This little snippet extends Array with a count method. It takes
> a code block that can be used to count elements that match a specific
> condition:
>
> a = Array[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
> puts a.count { |v| v < 5 }
a = (1..10).to_a
puts a.find_all {|v| v < 5 }.size
4
Ian
--
Ian Macdonald | Crazee Edeee, his prices are INSANE!!!
System Administrator |
[email]ian@caliban.org[/email] |
[url]http://www.caliban.org[/url] |
|
Ian Macdonald Guest
-
Email Site-Wide Error message (with code snippet)
I would like to create a Site-Wide error message that is emailed to the administrator. I have a lot of the information I want to put in but I can... -
A code snippet: Controlled retry
I sometimes find myself retrying operations when in a networked situation (e.g., maybe server isn't up, just try again). I just wrote this little... -
[ba-rb] BA-rb ( Bay Area Ruby Users Group ) - 'Generating Code in Ruby' by Jack Herrington.
Count me in again, too. -- Jos Backus _/ _/_/_/ Sunnyvale, CA _/ _/ _/ _/ _/_/_/ _/ _/ _/ _/ jos at... -
BA-rb ( Bay Area Ruby Users Group ) - 'Generating Code in Ruby' by Jack Herrington.
BA-rb (Bay Area Ruby language Users Group) is pleased to announce that it will begin meeting again. The topic for the first meeting will be a... -
Code Snippet: Array.shuffle
Here is another small Ruby code snippet that extends the Array class. Use this to randomize the order of all entries in an Array. class Array... -
Stefan Arentz #2
Re: Ruby Code Snippet: Array.count
Ian Macdonald <ian@caliban.org> writes:
Yes but this is very inefficient if you have large collections. My version> On Sat 05 Jul 2003 at 08:01:38 +0900, Stefan Arentz wrote:
>>> >
> > Maybe this functionality is already somewhere in Ruby, but I could not
> > find it. This little snippet extends Array with a count method. It takes
> > a code block that can be used to count elements that match a specific
> > condition:
> >
> > a = Array[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
> > puts a.count { |v| v < 5 }
> This is already available with Enumerable#find_all:
>
> a = (1..10).to_a
> puts a.find_all {|v| v < 5 }.size
>
> 4
does not need to create a new temporary collection just to count it.
S.
Stefan Arentz Guest
-
Dave Thomas #3
Re: Ruby Code Snippet: Array.count
Stefan Arentz wrote:
I'm starting to like using 'inject' for this kind of thing, but it isn't> Maybe this functionality is already somewhere in Ruby, but I could not
> find it. This little snippet extends Array with a count method. It takes
> a code block that can be used to count elements that match a specific
> condition:
>
> a = Array[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
> puts a.count { |v| v < 5 }
>
> ==> 4
>
> The code:
>
> class Array
> def count(&action)
> count = 0
> self.each { |v| count = count + 1 if action.call(v) }
> return count
> end
> end
as clear to read:
class Array
def count
inject(0) {|sum, e| yield(e) ? sum + 1 : sum }
end
end
> Ruby is sweet :-)
Agreed
Cheers
Dave
Dave Thomas Guest
-
Simon Strandgaard #4
Re: Ruby Code Snippet: Array.count
On Sat, 05 Jul 2003 14:20:35 +0900, Dave Thomas wrote:
>
> class Array
> def count
> inject(0) {|sum, e| yield(e) ? sum + 1 : sum }
> end
> end
I have added your Array#count (using inject), here:
[url]http://www.rubygarden.org/ruby?StandardClassExtensions/Array[/url]
--
Simon Strandgaard
Simon Strandgaard Guest
-
Mauricio Fernández #5
Re: Ruby Code Snippet: Array.count
On Sat, Jul 05, 2003 at 03:43:00PM +0900, Simon Strandgaard wrote:
The other version is faster IMHO, and in this case at least as readable:> On Sat, 05 Jul 2003 14:20:35 +0900, Dave Thomas wrote:>> >
> > class Array
> > def count
> > inject(0) {|sum, e| yield(e) ? sum + 1 : sum }
> > end
> > end
>
> I have added your Array#count (using inject), here:
>
> [url]http://www.rubygarden.org/ruby?StandardClassExtensions/Array[/url]
class Array
def count
sum = 0
each { |x| sum += 1 if yield x }
sum
end
end
--
_ _
| |__ __ _| |_ ___ _ __ ___ __ _ _ __
| '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \
| |_) | (_| | |_\__ \ | | | | | (_| | | | |
|_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com
'Ooohh.. "FreeBSD is faster over loopback, when compared to Linux
over the wire". Film at 11.'
-- Linus Torvalds
Mauricio Fernández Guest
-
Dave Thomas #6
Re: Ruby Code Snippet: Array.count
Simon Strandgaard wrote:
My code was written in response to the OP's. However, if you're going to> On Sat, 05 Jul 2003 14:20:35 +0900, Dave Thomas wrote:
>>>> class Array
>> def count
>> inject(0) {|sum, e| yield(e) ? sum + 1 : sum }
>> end
>> end
>
>
> I have added your Array#count (using inject), here:
save it for posterity, you should really put it under Enumerable.
Dave
Dave Thomas Guest
-
Dave Thomas #7
Re: Ruby Code Snippet: Array.count
Mauricio Fernández wrote:
Agreed (in fact I believe I said the original version was more> The other version is faster IMHO, and in this case at least as readable:
readable). I just wanted to post an alternative to start introducing
#inject...
Cheers
Dave
Dave Thomas Guest
-
Mike Stok #8
Re: Ruby Code Snippet: Array.count
In article <be61dp$j6g$03$1@news.t-online.com>,
Volker Grabsch <volker_grabsch@v.notjusthosting.com> wrote:
Once you assimilate the idiom they aren't too bad. Readability only>Injects are *never* clear to read, are they?
"happens" when a reader looks at something, I don't think it's an
inherent property of the material being read.
Mike
--
[email]mike@stok.co.uk[/email] | The "`Stok' disclaimers" apply.
[url]http://www.stok.co.uk/~mike/[/url] | GPG PGP Key 1024D/059913DA
[email]mike@exegenix.com[/email] | Fingerprint 0570 71CD 6790 7C28 3D60
[url]http://www.exegenix.com/[/url] | 75D2 9EC4 C1C0 0599 13DA
Mike Stok Guest



Reply With Quote

