所以我正在爲ruby dev bootcamp做準備工作,並且需要創建一個能夠使標題大寫的程序。我目前的代碼使用#capitalize!
,但是當一個數字包含在字符串中時,它被省略了。紅寶石如何使用#capitalize!並保留字符串編號
words = title.split(' ')
words.map! do |word|
if %w(a aboard about above absent across after against along alongside amid amidst among amongst an and around as aslant astride at
athwart atop barring before behind below beneath beside besides between beyond but by despite down during except for from in inside
into like mid minus near next nor notwithstanding of off on onto opposite or out outside over past per plus regarding round save
since so than the through throughout till times to toward towards under underneath unlike until up upon via vs. when with within
without worth yet).include?(word) && word != words[0]
word
else
word.capitalize!
end
所以當what I wish I knew when I was 20
輸入我得到What I Wish I Knew When I Was
有什麼建議?
你能舉個例子嗎? 'a ='hello3'; a.capitalize!'將'a'變成'Hello3'。這對你來說有什麼不同? – Matt