Skip to content

Instantly share code, notes, and snippets.

@brianmcgue
Created January 9, 2020 02:45
Show Gist options
  • Save brianmcgue/c65c95098ecf4d763e61ede64e08fd6a to your computer and use it in GitHub Desktop.
Save brianmcgue/c65c95098ecf4d763e61ede64e08fd6a to your computer and use it in GitHub Desktop.
def spongebob_case(string)
string.split('.').map do |sentence|
capital = false
sentence.strip.split.map do |word|
if word.downcase == 'i'
'I'
else
capital = false
word.each_char.map do |char|
if char.downcase == 'l'
capital = false
'L'
elsif char.downcase == 'i'
capital = true
'i'
else
capital = !capital
capital ? char.downcase : char.upcase
end
end.join('')
end
end.join(' ')
end.join('. ')
end
def test_input(input, expected)
actual = spongebob_case(input)
unless actual == expected
puts "expected: #{expected.inspect}"
puts " actual: #{actual.inspect}"
end
end
test_input('the quick brown fox jumps over the lazy dog', 'tHe qUiCk bRoWn fOx jUmPs oVeR tHe LaZy dOg')
test_input('i love pie', 'I LoVe piE')
test_input('billiards', 'biLLiArDs')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment