Created
October 17, 2022 10:44
-
-
Save thiagofm/0e7ca80b0d8c4f73d6d727de10d6a4dc to your computer and use it in GitHub Desktop.
include ruby table flip
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
# Let's define a FlippedTable class | |
module FlippedTable | |
# Define method we want to use in different classes | |
def table | |
"┻━┻" | |
end | |
end | |
class AngryTableFlipper | |
# Include it to a class that will use it | |
include FlippedTable | |
def table_flip | |
# Call table method from Flipped Table module | |
"(ノಠ益ಠ)ノ彡" + table | |
end | |
end | |
AngryTableFlipper.new.table_flip | |
# => "(ノಠ益ಠ)ノ彡┻━┻" | |
# Why is this better for Gems? | |
# A class in Ruby can only inherit from a single class | |
# But can "include" from many classes... 😌 | |
# Allowing us for greater abstractions! | |
class TableFlipBase # Let's generalize table flipping | |
def table_flip | |
flipper + table | |
end | |
end | |
module FlippingBear | |
# Define method we want to use in different classes | |
def flipper | |
"ʕノ•ᴥ•ʔノ ︵" | |
end | |
end | |
class BearFlippingTable < TableFlipBase | |
include FlippedTable | |
include FlippingBear | |
end # No code needed, just abstractions used! | |
BearFlippingTable.new.table_flip | |
# => "ʕノ•ᴥ•ʔノ ︵┻━┻" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment