Last active
September 24, 2021 15:07
-
-
Save Themitchell/8ee92d3d86452578d79339162986f9fd to your computer and use it in GitHub Desktop.
Object Oriented Programming Workshop
This file contains 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
def sum(a, b) | |
return a + b | |
end | |
class Sum | |
attr_writer :a | |
def initialize(a, b) | |
@a = a | |
@b = b | |
end | |
def perform | |
return @a + @b | |
end | |
end | |
class Animal | |
attr_accessor :has_backbone, | |
:cold_blooded | |
DEFAULT_NUM_LEGS = 4.freeze | |
def number_of_legs | |
DEFAULT_NUM_LEGS | |
end | |
end | |
class Mammal < Animal | |
def has_backbone | |
true | |
end | |
end | |
dog = Mammal.new | |
dog.has_backbone = true | |
dog.cold_blooded = false | |
class Human < Mammal | |
attr_reader :skin_colour | |
def initialize(skin_colour: nil) | |
@skin_colour = skin_colour | |
end | |
def cold_blooded | |
"what are you crazy" | |
end | |
end | |
human = Human.new | |
human.has_backbone = false | |
human.cold_blooded = true | |
module FishMethods | |
def has_gills? | |
true | |
end | |
end | |
class Fish < Animal | |
include FishMethods | |
def number_of_legs | |
0 | |
end | |
end | |
fish = Fish.new | |
fish.has_backbone = true | |
fish.cold_blooded = true | |
class Aquaman < Human | |
include FishMethods | |
end | |
aquaman = Aquaman.new | |
animal = Animal.new | |
def method_1(other_method) | |
p "starting method 1" | |
other_method.call | |
p "ending method 1" | |
nil | |
end | |
def method_2 | |
p "W00 i made it to method 2" | |
nil | |
end | |
method_1(-> { method_2 }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment