Ruby’s Enumerable Module: The Inject Method

by woody2shoes on July 16, 2010


Quick Ruby Tip: The inject method is useful for aggregating data across a dataset. For example a summation could be done with inject (by aggregating each number into the sum.)
Download 49.1 MB
Download (iphone & ipod) 7.6 MB

  • Anonymous

    The sound cuts out for me at around 50 seconds in, curious if anyone else is having this issue

    • Luke

      me too

      • http://teachmetocode.com Charles Max Wood

        I’ve re-encoded and re-uploaded the video. Let me know if it’s better. (It’s working now for me.)

    • http://teachmetocode.com Charles Max Wood

      I’ve re-encoded and re-uploaded the video. Let me know if it’s better. (It’s working now for me.)

  • Getbuys

    same here.
    about the cast itself. you forgot to tell one important thing about inject’s initial value.
    it should be immediate (fixnum, etc) or you should initialize stuff using a block.

  • Getbuys

    what i mean is that if you have array of hashes and want to regroup it by some fields

    [{:id=>3, :group=>'test'},{:id=>12, :group=>'sr4'},{:id=>5, :group=>'test'}].inject(Hash.new {|h,k| h[k] = Array.new}) {|h,el| h[el[:b]] <[{:group=>"test", :id=>3}, {:group=>"test", :id=>5}], “sr4″=>[{:group=>"sr4", :id=>12}]}
    as expected

  • Anonymous

    I am having the same issue — the video and the sound get choppy for me about 60 seconds in with some gnarly squealing.

    • http://teachmetocode.com Charles Max Wood

      I’ve re-encoded and re-uploaded the video. Let me know if it’s better. (It’s working now for me.)

  • Andrius Chamentauskas

    Also this isn’t directly relevant, you probably should’ve mentioned putting variables in brackets for ruby to auto assign them. What I mean is you could do:
    hash.inject({}) {|memo, (key, value)| memo[value] = key }
    and get the same result as in screencast. I personally think this looks much better than what you showed in last example in screencast.

  • ersatzryan

    The sound cuts out for me at around 50 seconds in, curious if anyone else is having this issue

  • Getbuys

    same here.
    about the cast itself. you forgot to tell one important thing about inject's initial value.
    it should be immediate (fixnum, etc) or you should initialize stuff using a block.

  • Getbuys

    what i mean is that if you have array of hashes and want to regroup it by some fields

    [{:id=>3, :group=>'test'},{:id=>12, :group=>'sr4'},{:id=>5, :group=>'test'}].inject(Hash.new {|h,k| h[k] = Array.new}) {|h,el| h[el[:b]] << el; h}
    will produce
    {“test”=>[{:group=>"test", :id=>3}, {:group=>"test", :id=>5}], “sr4″=>[{:group=>"sr4", :id=>12}]}
    as expected

  • xunker

    I am having the same issue — the video and the sound get choppy for me about 60 seconds in with some gnarly squealing.

  • Andrius Chamentauskas

    Also this isn't directly relevant, you probably should've mentioned putting variables in brackets for ruby to auto assign them. What I mean is you could do:
    hash.inject({}) {|memo, (key, value)| memo[value] = key }
    and get the same result as in screencast. I personally think this looks much better than what you showed in last example in screencast.

  • Disqus

    yep – no sound for me either.

    Shame – i’d be interested to watch this one

    • http://teachmetocode.com Charles Max Wood

      I’ve re-encoded and re-uploaded the video. Let me know if it’s better. (It’s working now for me.)

  • Disqus

    yep – no sound for me either.

    Shame – i'd be interested to watch this one

  • Brett Goulder

    This screencast is broken :

    • http://teachmetocode.com Charles Max Wood

      I’ve re-encoded and re-uploaded the video. Let me know if it’s better. (It’s working now for me.)

  • Brett Goulder

    This screencast is broken :

  • cire

    Still have issue with the sound for me too.

    • http://teachmetocode.com Charles Max Wood

      I’ve re-encoded and re-uploaded the video. Let me know if it’s better. (It’s working now for me.)

  • cire

    Still have issue with the sound for me too.

  • Luke

    me too

  • http://teachmetocode.com Charles Max Wood

    I’ve re-encoded and re-uploaded the video. Let me know if it’s better. (It’s working now for me.)

  • woody2shoes

    I've re-encoded and re-uploaded the video. Let me know if it's better. (It's working now for me.)

  • woody2shoes

    I've re-encoded and re-uploaded the video. Let me know if it's better. (It's working now for me.)

  • woody2shoes

    I've re-encoded and re-uploaded the video. Let me know if it's better. (It's working now for me.)

  • woody2shoes

    I've re-encoded and re-uploaded the video. Let me know if it's better. (It's working now for me.)

  • woody2shoes

    I've re-encoded and re-uploaded the video. Let me know if it's better. (It's working now for me.)

  • woody2shoes

    I've re-encoded and re-uploaded the video. Let me know if it's better. (It's working now for me.)

  • woody2shoes

    I've re-encoded and re-uploaded the video. Let me know if it's better. (It's working now for me.)

  • Mike Bethany

    I still have audio problems. I’ve tried QuickTime player, Moviest, and VLC. All of them have the audio being garbled starting at 00:00:42, a horrible squelching at 00:00:49, then it goes silent. I have of course downloaded from the link above.

  • Mike Bethany

    I still have audio problems. I’ve tried QuickTime player, Moviest, and VLC. All of them have the audio being garbled starting at 00:00:42, a horrible squelching at 00:00:49, then it goes silent. I have of course downloaded from the link above.

  • Sm

    you can get key and value using…
    hash.inject([]) { |m, (key, value) |  …   }

Previous post:

Next post: