Skip to content

Instantly share code, notes, and snippets.

@oozzal
Last active August 29, 2015 14:06
Show Gist options
  • Save oozzal/f7bbc5a92c71ff88d14a to your computer and use it in GitHub Desktop.
Save oozzal/f7bbc5a92c71ff88d14a to your computer and use it in GitHub Desktop.
require 'delegate'
class Person
attr_accessor :fname, :lname
def category
'manav'
end
end
class Male < Person
def category
'vale'
end
end
class Female < Person
def category
'pothi'
end
end
class DecoratedPerson < SimpleDelegator
def initialize
@sexes = [Male.new, Female.new].cycle
super(Person.new)
end
def full_name
fname.to_s + ' ' + lname.to_s
end
def change_sex
__setobj__ @sexes.next
end
end
p = DecoratedPerson.new
puts p.category # 'manav'
p.change_sex
puts p.category # 'vale'
p.change_sex
puts p.category # 'pothi'
p.change_sex
puts p.category # 'vale'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment