Created
November 12, 2017 10:32
-
-
Save eiwi1101/29cfe832949bd5db61f0a3f0c62fa352 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Consider this common pattern... | |
if (user = User.find params[:id]) | |
user.profile_viewed | |
end | |
# Would be a bit less awkward if... | |
with User.find params[:id] do |user| | |
user.profile_viewed | |
end | |
# And all we need is... | |
def with(item) | |
yield item if item | |
end | |
# But what if we wanted iteration... | |
with_each User.all do |user| | |
user.profile_viewed | |
end | |
# Of course that's going too far... | |
# We already have Enumerable#each | |
# This is all just syntax sugar. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment