Last active
December 20, 2015 22:19
-
-
Save jordelver/6204041 to your computer and use it in GitHub Desktop.
Special case object
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
class User | |
def name | |
# ...from the database | |
"Fred" | |
end | |
def self.find(user_id) | |
user_id == 123 ? self.new : nil | |
end | |
end | |
class Guest | |
def name | |
"Anonymous" | |
end | |
end | |
def current_user(session) | |
User.find(session[:user_id]) || Guest.new | |
end | |
session = {} | |
session[:user_id] = 123 | |
p current_user(session).name | |
session[:user_id] = 'NOT VALID' | |
p current_user(session).name | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment