Created
January 26, 2016 04:56
-
-
Save unixmonkey/c307954ab57222b6c135 to your computer and use it in GitHub Desktop.
Object#smoosh
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
# Fun stuff for training purposes | |
# Demonstrates: | |
# Monkey-Patching | |
# Polymorphism | |
# Case statements | |
# Type matching | |
# Regular Expressions | |
# Raising errors | |
# Enumurable#map | |
# Symbol to Proc | |
# Recursion | |
# Testing | |
# Looping assertions | |
# 80's sitcom jingles | |
class Object | |
def smoosh | |
case self | |
when String | |
gsub(/[[:space:]]+/, '') | |
when Symbol | |
to_s.smoosh | |
when Array | |
map(&:smoosh).join | |
when Hash | |
map { |key, value| key.smoosh + value.smoosh }.smoosh | |
else | |
raise NotImplementedError | |
end | |
end | |
end | |
require 'minitest/autorun' | |
class TestSmoosh < Minitest::Test | |
def test_smoosh_smooshes_all_the_things | |
[ | |
['Sunday, Monday, Happy Days!', 'Sunday,Monday,HappyDays!'], | |
[['Tuesday', 'Wednesday', 'happy days'], 'TuesdayWednesdayhappydays'], | |
[{ thursday: 'Friday', happy: 'days!' }, 'thursdayFridayhappydays!'], | |
[{ saturday: { the: 'weekend comes', my: { cycle: 'hums' }, groovin: 'all week with you!' } }, 'saturdaytheweekendcomesmycyclehumsgroovinallweekwithyou!'], | |
].each do |input, expected_output| | |
assert_equal expected_output, input.smoosh | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment