Created
October 30, 2025 21:03
-
-
Save RobertoBarros/d8862206311b223c964daa63d95b8983 to your computer and use it in GitHub Desktop.
Batch 2166 - Livecode OOP
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
| class Animal | |
| attr_reader :name | |
| def initialize(name) | |
| @name = name | |
| end | |
| def self.phyla | |
| %w[Porifera Cnidaria Platyhelminthes Nematoda Annelida Arthropoda Mollusca Echinodermata Chordata] | |
| end | |
| def eat(food) | |
| "The #{@name} eats #{food}." | |
| end | |
| end | |
| ####################################### | |
| class Lion < Animal | |
| def talk | |
| "#{@name} roars" | |
| end | |
| def eat(food) | |
| "#{super} Law of the jungle!" | |
| end | |
| end | |
| ####################################### | |
| class Meerkat < Animal | |
| def talk | |
| "#{@name} bark" | |
| end | |
| end | |
| ###################################### | |
| class Warthog < Animal | |
| def talk | |
| "#{@name} grunt" | |
| end | |
| end | |
| ######################################### | |
| #### Using our Classes: | |
| simba = Lion.new("Simba") | |
| timao = Meerkat.new("Timão") | |
| pumba = Warthog.new("Pumba") | |
| nala = Lion.new("Nala") | |
| animals = [simba, timao, pumba, nala] | |
| animals.each do |a| | |
| puts a.talk | |
| end | |
| puts Animal.phyla | |
| puts timao.eat("escorpiao") | |
| puts simba.eat("gazela") | |
| puts pumba.eat("planta") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment