Ruby Code Snippet: Array.count

Ask a Question related to Ruby, Design and Development.

  1. #1

    Default Re: Ruby Code Snippet: Array.count

    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

    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

  2. Similar Questions and Discussions

    1. 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...
    2. 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...
    3. [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...
    4. 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...
    5. 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...
  3. #2

    Default Re: Ruby Code Snippet: Array.count

    Ian Macdonald <ian@caliban.org> writes:
    > 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
    Yes but this is very inefficient if you have large collections. My version
    does not need to create a new temporary collection just to count it.

    S.
    Stefan Arentz Guest

  4. #3

    Default Re: Ruby Code Snippet: Array.count

    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 }
    >
    > ==> 4
    >
    > The code:
    >
    > class Array
    > def count(&action)
    > count = 0
    > self.each { |v| count = count + 1 if action.call(v) }
    > return count
    > end
    > end
    I'm starting to like using 'inject' for this kind of thing, but it isn't
    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

  5. #4

    Default 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

  6. #5

    Default Re: Ruby Code Snippet: Array.count

    On Sat, Jul 05, 2003 at 03:43:00PM +0900, Simon Strandgaard wrote:
    > 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]
    The other version is faster IMHO, and in this case at least as readable:

    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

  7. #6

    Default Re: Ruby Code Snippet: Array.count

    Simon Strandgaard wrote:
    > 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:
    My code was written in response to the OP's. However, if you're going to
    save it for posterity, you should really put it under Enumerable.



    Dave


    Dave Thomas Guest

  8. #7

    Default Re: Ruby Code Snippet: Array.count

    Mauricio Fernández wrote:

    > The other version is faster IMHO, and in this case at least as readable:
    Agreed (in fact I believe I said the original version was more
    readable). I just wanted to post an alternative to start introducing
    #inject...

    Cheers


    Dave


    Dave Thomas Guest

  9. #8

    Default Re: Ruby Code Snippet: Array.count

    In article <be61dp$j6g$03$1@news.t-online.com>,
    Volker Grabsch <volker_grabsch@v.notjusthosting.com> wrote:
    >Injects are *never* clear to read, are they?
    Once you assimilate the idiom they aren't too bad. Readability only
    "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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139