Skip to content

Instantly share code, notes, and snippets.

@psyomn
Last active December 17, 2015 17:18
Show Gist options
  • Select an option

  • Save psyomn/5644557 to your computer and use it in GitHub Desktop.

Select an option

Save psyomn/5644557 to your computer and use it in GitHub Desktop.
Things I tend to forget in ruby.
#!/usr/bin/env ruby
arr = [
[:a, [1,2,2,2,2,1]],
[:b, [3,3,3,3,3,4,4,4]],
[:c, [8,8,8,8,8,8,8]]]
headers = arr.collect{|el| el[0]}
headers.each do |h|
puts h
p arr.group_by{|x| x[0] == h}[true][0]
end
arr = (1..100).to_a
arr.group_by{|i| i%3}
# Above
# => {1=>[1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40,
# 43, 46, 49, 52, 55, 58, 61, 64, 67, 70, 73, 76, 79,
# 82, 85, 88, 91, 94, 97, 100],
# 2=>[2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35, 38, 41,
# 44, 47, 50, 53, 56, 59, 62, 65, 68, 71, 74, 77, 80,
# 83, 86, 89, 92, 95, 98],
# 0=>[3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42,
# 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81,
# 84, 87, 90, 93, 96, 99]}
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment