ruby 钻头解决方案:Fun Strings
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 钻头解决方案:Fun Strings相关的知识,希望对你有一定的参考价值。
###FIRST ATTEMPT
def fun_stringitize(word)
string_array = word.split('')
string_array.each_with_index do |letter, index|
if (index.odd?)
letter.upcase!
end
end
string_array.join().reverse
end
###REFECTORED
def fun_stringitize(word)
word.gsub(/(\S)(\S)/) {$1 + $2.upcase}.reverse
end
###BONUS
class String
def fun_stringitize!
self.gsub!(/(\S)(\S)/) {$1 + $2.upcase}.reverse!
end
end
###DRIVER CODE/TESTS
string = 'apples'
puts fun_stringitize(string) == 'SeLpPa'
puts string == 'apples'
puts string.fun_stringitize! == 'SeLpPa'
puts string == 'SeLpPa'
以上是关于ruby 钻头解决方案:Fun Strings的主要内容,如果未能解决你的问题,请参考以下文章