Skip to content

Instantly share code, notes, and snippets.

@perplexes
Created December 30, 2009 01:12
Show Gist options
  • Save perplexes/265774 to your computer and use it in GitHub Desktop.
Save perplexes/265774 to your computer and use it in GitHub Desktop.
class Object
# Yield ourself to the block given, and let the block determine the value to pass.
# This is like #tap, but "alter"s the output instead of ignoring it.
# Useful for conditional scoping, like:
# # For admins, show all posts. For others, show only public posts.
# @posts = Post.all.alter{|p| current_user.admin? ? p : p.public}
#
# Rather than: note the double assignment :(
# @posts = Post.all
# @posts = @posts.public unless current_user.admin?
def alter
yield self
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment