Skip to content

Instantly share code, notes, and snippets.

@AnkurVyas-BTC
Created March 23, 2019 08:10
Show Gist options
  • Select an option

  • Save AnkurVyas-BTC/6288235cf4f0d52dcea87136d081c5c1 to your computer and use it in GitHub Desktop.

Select an option

Save AnkurVyas-BTC/6288235cf4f0d52dcea87136d081c5c1 to your computer and use it in GitHub Desktop.
# parent class (main)
class Human
def talk
''
end
def height
''
end
end
# the subtypes
class HomoHabilis < Human
def talk
'Agrrr!'
end
def height
'1.29m'
end
end
class HomoSapiens < Human
def talk
'Hello!'
end
def height
'1.70m'
end
end
# now we can use subtypes instead of Human
habilis = HomoHabilis.new
sapiens = HomoSapiens.new
def introduce_human(human)
puts "Hi, I'm #{human.height} height and I say #{human.talk}"
end
introduce_human(habilis) # => Hi, I'm 1.29m height and I say Agrrr!
introduce_human(sapiens) # => Hi, I'm 1.70m height and I say Hello!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment