Your SlideShare is downloading. ×
  • Like
Grouping (MOTM 2010.02)
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Grouping (MOTM 2010.02)

  • 406 views
Published

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

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

Published in Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
406
On SlideShare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
1
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide





























Transcript

  • 1. Gr ou pi ng Ruby Method of the Month Kevin Munc - @muncman
  • 2. in_groups in_groups_of
  • 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. Arrays into Groups
  • 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. 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. in_groups seq.in_groups(4) => [ ["turquoise", "ultramarine", "chartreuse", "lavender"], ["crimson", "burgundy", "coral", "teal"], ["magenta", "maroon", "cerulean", "fuchsia"], ["emerald", "taupe", "auburn", "vermillion"] ]
  • 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. 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. 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. 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. 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. 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. 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. Warning: The next two slides have Information Overload
  • 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. 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. Bonus Method!
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. Questions? Image Credits all from clickykbd@flickr http://www.flickr.com/photos/clickykbd