Created
March 23, 2019 08:10
-
-
Save AnkurVyas-BTC/6288235cf4f0d52dcea87136d081c5c1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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