Created
May 6, 2018 15:06
-
-
Save soonernotfaster/c2c12aaafee07359ae6bbba155de49a0 to your computer and use it in GitHub Desktop.
Don't Repeat Yourself
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
| def random_person | |
| names = %w(Mike Zoe Adam Sarah) | |
| jobs = %w(Astronaut Engineer Painter Day-Trader Theoretical-Physicist) | |
| Person.new(random_item(names), random_item(jobs)) | |
| end |
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
| def random_index(list) | |
| rand(4) | |
| end | |
| def random_item(list) | |
| list[random_index(list)] | |
| end |
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
| def random_person | |
| names = %w(Mike Zoe Adam Sarah) | |
| jobs = { | |
| stem: %w(Astronaut Engineer Theoretical-Physicist), | |
| finance: %w(Day-Trader), | |
| arts: %w(Painter) | |
| } | |
| Person.new(random_item(names), random_item(jobs)) | |
| end |
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
| def random_person | |
| # ... | |
| pets = [ | |
| { type: 'Dog', name: 'Fido'}, | |
| { type: 'Cat', name: 'Whiskers' }, | |
| { type: 'Horse', name: 'Thunder' } | |
| ] | |
| Person.new(random_item(names), random_item(jobs), random_item(pets)) | |
| end |
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 Person | |
| def initialize(name, job) | |
| @name = name | |
| @job = job | |
| end | |
| def to_s | |
| "Person named #{@name}, works as a #{@job}" | |
| end | |
| end | |
| def random_person | |
| names = %w(Mike Zoe Adam Sarah) | |
| jobs = %w(Astronaut Engineer Painter Day-Trader) | |
| Person.new(names[rand(4)], jobs[rand(4)]) | |
| end | |
| def main | |
| 10 | |
| .times | |
| .collect { random_person } | |
| .each { |p| puts p } | |
| end | |
| main |
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
| def random_index | |
| rand(4) | |
| end | |
| def random_person | |
| names = %w(Mike Zoe Adam Sarah) | |
| jobs = %w(Astronaut Engineer Painter Day-Trader) | |
| Person.new(names[random_index], jobs[random_index]) | |
| end |
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
| def random_item(list) | |
| list[random_index] | |
| end | |
| def random_person | |
| names = %w(Mike Zoe Adam Sarah) | |
| jobs = %w(Astronaut Engineer Painter Day-Trader) | |
| Person.new(random_item(names), random_item(jobs)) | |
| end |
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
| def random_index(list) | |
| rand(list.size) | |
| end |
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
| def random_item(list) | |
| if list.is_a?(Array) | |
| list[random_index(list)] | |
| else | |
| jobs = list.map {|_, values| values}.flatten | |
| jobs[random_index(jobs)] | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment