Skip to content

Instantly share code, notes, and snippets.

Created January 16, 2021 10:41
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
array exercises
# Two alternative approaches for
todos = [
["Send invoice", "money"],
["Clean room", "organize"],
["Pay rent", "money"],
["Arrange books", "organize"],
["Pay taxes", "money"],
["Buy groceries", "food"]
categories = {|todo| todo[1] }.uniq
# A functional approach
categories.each do |category|
puts "#{category}:"
todo_names =
todos.filter {|todo| todo[1] == category}.
map {|todo| " #{todo[0]}"}.
puts todo_names
# A more imperative approach
categories.each do |category|
puts "#{category}:"
todos_for_this_category = todos.filter {|todo| todo[1] == category}
todos_for_this_category.each do |todo|
puts " #{todo[0]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment