2

我正在制作一种将我的输入大写的方法,除了 a、an 和...等任何单词。

def titleize(string_to_titleize)
    string_to_titleize.split(' ').map { |words| words.capitalize }.join(' ')
end

我知道有宝石可以做到这一点。我无法掌握如何手动完成。我假设创建一个不大写的单词列表。然后,将他们排除在外。

4

1 回答 1

4
arr = ['a', 'an', 'the']
str ="This is a salil gaikwad working as an engineer"
str.gsub(/\w+/) {|match| arr.include?(match) ? match : match.capitalize} 
#Gives o/p :- This Is a Salil Gaikwad Working As an Engineer
于 2012-12-11T05:04:24.870 回答