Skip to content

Instantly share code, notes, and snippets.

@mikepack
Created February 11, 2015 05:02
Show Gist options
  • Save mikepack/c2e02505ecff4acd1edc to your computer and use it in GitHub Desktop.
Save mikepack/c2e02505ecff4acd1edc to your computer and use it in GitHub Desktop.
Polymorphic Null Object
class User
def name
'Mike Pack'
end
end
class GuestUser
def name
'Guest User'
end
end
class AdminUser
def name
'Admin User'
end
end
class Controller
def current_user
return GuestUser.new if rand(0..1) == 0
return AdminUser.new if rand(0..5) == 0
User.new
end
def show
puts current_user.name
end
end
Controller.new.show
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment