Grouping (MOTM 2010.02)

600 views

Published on

Ruby Method of the Month 2010.02. Lightning talk for the Columbus Ruby Brigade.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
600
On SlideShare
0
From Embeds
0
Number of Embeds
10
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide





























  • Grouping (MOTM 2010.02)

    1. 1. Gr ou pi ng Ruby Method of the Month Kevin Munc - @muncman
    2. 2. in_groups in_groups_of
    3. 3. r o s t p n p o e e u si S n t c Exiv t A e y a g r o :Ar i r n : :C : p r o u : :G http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Array/Grouping.html
    4. 4. Arrays into Groups
    5. 5. Follow Along in IRB! • You’re going to need to use ActiveSupport: •require ‘active_support’ • Note: require ‘activesupport’ (no underscore) is deprecated for removal in Rails 3.
    6. 6. A Colorful Array seq = %w{turquoise ultramarine chartreuse lavender crimson burgundy coral teal magenta maroon cerulean fuchsia emerald taupe auburn vermillion} seq.size => 16
    7. 7. in_groups seq.in_groups(4) => [ ["turquoise", "ultramarine", "chartreuse", "lavender"], ["crimson", "burgundy", "coral", "teal"], ["magenta", "maroon", "cerulean", "fuchsia"], ["emerald", "taupe", "auburn", "vermillion"] ]
    8. 8. in_groups_of seq.in_groups_of(4) => [ ["turquoise", "ultramarine", "chartreuse", "lavender"], ["crimson", "burgundy", "coral", "teal"], ["magenta", "maroon", "cerulean", "fuchsia"], ["emerald", "taupe", "auburn", "vermillion"] ]
    9. 9. in_groups seq.in_groups(3) => [ ["turquoise", "ultramarine", "chartreuse", "lavender", "crimson", "burgundy"], ["coral", "teal", "magenta", "maroon", "cerulean", nil], ["fuchsia", "emerald", "taupe", "auburn", "vermillion", nil] ]
    10. 10. in_groups_of seq.in_groups_of(3) => [ ["turquoise", "ultramarine", "chartreuse"], ["lavender", "crimson", "burgundy"], ["coral", "teal", "magenta"], ["maroon", "cerulean", "fuchsia"], ["emerald", "taupe", "auburn"], ["vermillion", nil, nil] ]
    11. 11. in_groups with false seq.in_groups(3, false) => [ ["turquoise", "ultramarine", "chartreuse", "lavender", "crimson", "burgundy"], ["coral", "teal", "magenta", "maroon", "cerulean"], ["fuchsia", "emerald", "taupe", "auburn", "vermillion"] ]
    12. 12. in_groups_of with false seq.in_groups_of(3, false) => [ ["turquoise", "ultramarine", "chartreuse"], ["lavender", "crimson", "burgundy"], ["coral", "teal", "magenta"], ["maroon", "cerulean", "fuchsia"], ["emerald", "taupe", "auburn"], ["vermillion"] ]
    13. 13. in_groups with filler seq.in_groups(3, 'white') => [ ["turquoise", "ultramarine", "chartreuse", "lavender", "crimson", "burgundy"], ["coral", "teal", "magenta", "maroon", "cerulean", "white"], ["fuchsia", "emerald", "taupe", "auburn", "vermillion", "white"] ]
    14. 14. in_groups_of with filler seq.in_groups_of(3, 'white') => [ ["turquoise", "ultramarine", "chartreuse"], ["lavender", "crimson", "burgundy"], ["coral", "teal", "magenta"], ["maroon", "cerulean", "fuchsia"], ["emerald", "taupe", "auburn"], ["vermillion", "white", "white"] ]
    15. 15. Warning: The next two slides have Information Overload
    16. 16. Another Look: in_groups gen = []; 20.times { |i| gen << i+1 }; gen => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] gen.in_groups(4) # into four groups => [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20]] gen.in_groups(3) # into three groups => [[1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13, 14], [15, 16, 17, 18, 19, 20, nil]] gen.in_groups(3, false) # into three groups, unpadded => [[1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13, 14], [15, 16, 17, 18, 19, 20]] gen.in_groups(3, 99) # into three groups, with filler => [[1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13, 14], [15, 16, 17, 18, 19, 20, 99]]
    17. 17. Another Look: in_groups_of gen = []; 20.times { |i| gen << i+1 }; gen => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] gen.in_groups_of(4) # into groups of four => [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16], [17, 18, 19, 20]] gen.in_groups_of(3) # into groups of three => [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18], [19, 20, nil]] gen.in_groups_of(3, false) # into groups of three, unpadded => [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18], [19, 20]] gen.in_groups_of(3, 99) # into groups of three, with filler => [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18], [19, 20, 99]]
    18. 18. Bonus Method!
    19. 19. split • Divide an array based on: • delimiter • or block • but not both • a block is used if present • This is the third and final method in ActiveSupport::CoreExtensions::Array:: Grouping
    20. 20. split it. split it good! a_ray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] a_ray.split(5) => [[1, 2, 3, 4], [6, 7, 8, 9, 10]]
    21. 21. split it. split it good! a_ray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] a_ray.split(5) => [[1, 2, 3, 4], [6, 7, 8, 9, 10]] b_ray = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5] b_ray.split(3) => [[1, 2], [4, 5, 1, 2], [4, 5]]
    22. 22. split it like it’s hot a_ray => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] a_ray.split { |elem| elem % 2 == 0 } => [[1], [3], [5], [7], [9], []]
    23. 23. split it like it’s hot a_ray => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] a_ray.split { |elem| elem % 2 == 0 } => [[1], [3], [5], [7], [9], []] a_ray => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] a_ray.split { |elem| elem % 2 == 1 } => [[], [2], [4], [6], [8], [10]]
    24. 24. splitting hairs b_ray => [1, 2, 3, 4, 5, 1, 2, 3, 4, 5] b_ray.split do |elem| (3..5).include? elem end => [[1, 2], [], [], [1, 2], [], [], []] # File activesupport/lib/active_support/core_ext/array/grouping.rb, line 90 90: def split(value = nil) 91: using_block = block_given? 92: 93: inject([[]]) do |results, element| 94: if (using_block && yield(element)) || (value == element) 95: results << [] 96: else 97: results.last << element 98: end 99: 100: results
    25. 25. splitting hairs b_ray => [1, 2, 3, 4, 5, 1, 2, 3, 4, 5] Matches are excluded, starting a new array group. b_ray.split do |elem| (3..5).include? elem end => [[1, 2], [], [], [1, 2], [], [], []] # File activesupport/lib/active_support/core_ext/array/grouping.rb, line 90 90: def split(value = nil) 91: using_block = block_given? 92: 93: inject([[]]) do |results, element| 94: if (using_block && yield(element)) || (value == element) 95: results << [] 96: else 97: results.last << element 98: end 99: 100: results
    26. 26. splitting hairs b_ray => [1, 2, 3, 4, 5, 1, 2, 3, 4, 5] Matches are excluded, starting a new array group. b_ray.split do |elem| (3..5).include? elem end => [[1, 2], [], [], [1, 2], [], [], []] 3 # File activesupport/lib/active_support/core_ext/array/grouping.rb, line 90 90: def split(value = nil) 91: using_block = block_given? 92: 93: inject([[]]) do |results, element| 94: if (using_block && yield(element)) || (value == element) 95: results << [] 96: else 97: results.last << element 98: end 99: 100: results
    27. 27. splitting hairs b_ray => [1, 2, 3, 4, 5, 1, 2, 3, 4, 5] Matches are excluded, starting a new array group. b_ray.split do |elem| (3..5).include? elem end => [[1, 2], [], [], [1, 2], [], [], []] 3 4 # File activesupport/lib/active_support/core_ext/array/grouping.rb, line 90 90: def split(value = nil) 91: using_block = block_given? 92: 93: inject([[]]) do |results, element| 94: if (using_block && yield(element)) || (value == element) 95: results << [] 96: else 97: results.last << element 98: end 99: 100: results
    28. 28. splitting hairs b_ray => [1, 2, 3, 4, 5, 1, 2, 3, 4, 5] Matches are excluded, starting a new array group. b_ray.split do |elem| (3..5).include? elem end => [[1, 2], [], [], [1, 2], [], [], []] 3 4 5 # File activesupport/lib/active_support/core_ext/array/grouping.rb, line 90 90: def split(value = nil) 91: using_block = block_given? 92: 93: inject([[]]) do |results, element| 94: if (using_block && yield(element)) || (value == element) 95: results << [] 96: else 97: results.last << element 98: end 99: 100: results
    29. 29. splitting hairs b_ray => [1, 2, 3, 4, 5, 1, 2, 3, 4, 5] Matches are excluded, starting a new array group. b_ray.split do |elem| (3..5).include? elem end => [[1, 2], [], [], [1, 2], [], [], []] 3 4 5 3 # File activesupport/lib/active_support/core_ext/array/grouping.rb, line 90 90: def split(value = nil) 91: using_block = block_given? 92: 93: inject([[]]) do |results, element| 94: if (using_block && yield(element)) || (value == element) 95: results << [] 96: else 97: results.last << element 98: end 99: 100: results
    30. 30. splitting hairs b_ray => [1, 2, 3, 4, 5, 1, 2, 3, 4, 5] Matches are excluded, starting a new array group. b_ray.split do |elem| (3..5).include? elem end => [[1, 2], [], [], [1, 2], [], [], []] 3 4 5 3 4 # File activesupport/lib/active_support/core_ext/array/grouping.rb, line 90 90: def split(value = nil) 91: using_block = block_given? 92: 93: inject([[]]) do |results, element| 94: if (using_block && yield(element)) || (value == element) 95: results << [] 96: else 97: results.last << element 98: end 99: 100: results
    31. 31. splitting hairs b_ray => [1, 2, 3, 4, 5, 1, 2, 3, 4, 5] Matches are excluded, starting a new array group. b_ray.split do |elem| (3..5).include? elem end => [[1, 2], [], [], [1, 2], [], [], []] 3 4 5 3 4 5 # File activesupport/lib/active_support/core_ext/array/grouping.rb, line 90 90: def split(value = nil) 91: using_block = block_given? 92: 93: inject([[]]) do |results, element| 94: if (using_block && yield(element)) || (value == element) 95: results << [] 96: else 97: results.last << element 98: end 99: 100: results
    32. 32. Questions? Image Credits all from clickykbd@flickr http://www.flickr.com/photos/clickykbd

    ×