Skip to content

Instantly share code, notes, and snippets.

@katakeynii
Last active October 25, 2022 11:10
Show Gist options
  • Save katakeynii/7fce4dd9ba514b961dad1efc1539786b to your computer and use it in GitHub Desktop.
Save katakeynii/7fce4dd9ba514b961dad1efc1539786b to your computer and use it in GitHub Desktop.
Testing delegate 2
require "delegate"
class User
attr_accessor :firstname, :lastname, :username
def initialize(firstname, lastname, username)
@firstname = firstname
@lastname = lastname
@username = username
end
def iam
"Hi! I am #{firstname} #{lastname} (#{username})"
end
end
class PersonalProfile < DelegateClass(User)
attr_accessor :cover_url, :posts, :followers, :followings
def initialize user
@posts = []
@followers = []
@followings = []
super(user)
end
end
class CorporateProfile < DelegateClass(User)
attr_accessor :cover_url, :posts, :followers, :followings, :curriculum
def initialize user
@posts = []
@followers = []
@followings = []
super(user)
end
end
tonux = User.new "Tonux", "Samb", "tonux"
tonuxLab = PersonalProfile.new tonux
takkJok = PersonalProfile.new tonux
tonuxCorp = CorporateProfile.new tonux
puts tonuxLab.firstname # Tonux
puts takkJok.iam # Hi! I am Tonux Samb (tonux)
puts tonuxCorp.firstname # Tonux
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment