Skip to content

Instantly share code, notes, and snippets.

@wowremywang
Last active October 8, 2021 20:02
Show Gist options
  • Save wowremywang/cae159456cece85fd3876791c1cea877 to your computer and use it in GitHub Desktop.
Save wowremywang/cae159456cece85fd3876791c1cea877 to your computer and use it in GitHub Desktop.
Helpful methods
each_with_object
## Reference: https://womanonrails.com/each-with-object

# Bad
array = []
(1..10).each do |item|
  array << item ** 2
end
array

# Good
(1..10).each_with_object([]) do |item, array|
  array << item ** 2
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment