Created
August 9, 2020 07:57
-
-
Save dearshrewdwit/04fe99eef3d1a649802f74eaed9d9198 to your computer and use it in GitHub Desktop.
Working with multiple classes
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 AssertionList | |
| # write a method #run that iterates through a list of assertions | |
| # and asks each one to run its #check method and then #puts the result. | |
| end | |
| class Equal | |
| def initialize(val1, val2) | |
| @val1 = val1 | |
| @val2 = val2 | |
| end | |
| def check | |
| @val1 == @val2 | |
| end | |
| end | |
| class Empty | |
| def initialize(val) | |
| @val = val | |
| end | |
| def check | |
| @val.length == 0 | |
| end | |
| end | |
| test1 = Equal.new(1, 1) | |
| test2 = Equal.new(1, 2) | |
| test3 = Empty.new([1, 2]) | |
| test4 = Empty.new({}) | |
| assertion_list = AssertionList.new([test1, test2, test3, test4]) | |
| assertion_list.run | |
| # true | |
| # false | |
| # false | |
| # true | |
| # Further Work | |
| assertion_list2 = AssertionList.new(Equal, Empty) | |
| assertion_list2.create_equal(4,5) # creates an instance of Equal and stores it | |
| assertion_list2.create_empty([]) # creates an instance of Equal and stores it | |
| assertion_list2.run | |
| # false | |
| # true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment