Skip to content

Instantly share code, notes, and snippets.

@betogrun
Last active January 11, 2018 16:27
Show Gist options
  • Save betogrun/49f3ec29d3561dcff53a83ee7e2eb706 to your computer and use it in GitHub Desktop.
Save betogrun/49f3ec29d3561dcff53a83ee7e2eb706 to your computer and use it in GitHub Desktop.
Even or Odd
array = [0,1,2,3,4,5,6,7,8,9]
array.each_with_object({:odd => [], :even => []}) do |item, memo|
memo[item.odd? ? :odd : :even] << item
end
@betogrun
Copy link
Author

Alguma forma melhor de separar os pares dos ímpares?

@betogrun
Copy link
Author

Using group_by

array = [0,1,2,3,4,5,6,7,8,9]
array.group_by {|n| n.even? ? :even : :odd }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment