Skip to content

Instantly share code, notes, and snippets.

@GertThiel
Created January 28, 2009 08:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GertThiel/53862 to your computer and use it in GitHub Desktop.
Save GertThiel/53862 to your computer and use it in GitHub Desktop.
class UserSession
def viewable_posts
# By using Post.scoped({}) rather than Post.all, you get a named scope proxy back,
# meaning that you can chain other scopes onto it
# and that the query is not executed until necessary.
admin? ? Post.scoped({}) : Post.published
end
end
class User
def pastor?
roles.any? { |r| r.title.include?('Pastor') }
end
end
User.all.each do |user|
# do something with “user”
end
# Greedy vs. non-greedy reg exp
i = 'acdbcdb'
i.match(/a.+b/)[0] # greedy, yields “acdbcdb”
i.match(/a.+?b/)[0] # non-greedy, yields “acdb”
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment