Skip to content

Instantly share code, notes, and snippets.

@eiwi1101
Created November 12, 2017 10:32
Show Gist options
  • Save eiwi1101/29cfe832949bd5db61f0a3f0c62fa352 to your computer and use it in GitHub Desktop.
Save eiwi1101/29cfe832949bd5db61f0a3f0c62fa352 to your computer and use it in GitHub Desktop.
# 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