Advertisement
Guest User

Untitled

a guest
Apr 15th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.43 KB | None | 0 0
  1. class User
  2.   def initialize(admin: true)
  3.     @admin = admin
  4.   end
  5.  
  6.   attr_reader :admin
  7.  
  8.   def admin?
  9.     admin == true
  10.   end
  11. end
  12.  
  13. class Foo
  14.   def initialize(user)
  15.     @user = user
  16.   end
  17.  
  18.   def foo
  19.     @foo ||= @user.admin? ? :do_admin : :do_normal
  20.   end
  21.  
  22.   def do_admin
  23.     'i am an admin'
  24.   end
  25.  
  26.   def do_normal
  27.     'i am not an admin'
  28.   end
  29. end
  30.  
  31. u = User.new(admin: false)
  32. f = Foo.new(u)
  33. puts f.send(f.foo)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement