我正在制作一种方法来大写我的输入,除了a,an和...之类的任何单词。
def titleize(string_to_titleize)
string_to_titleize.split(' ').map { |words| words.capitalize }.join(' ')
end
我知道有宝石可以做到这一点。我无法掌握如何手动完成。我假设创建一个不被大写的单词列表。然后,将他们排除在外。
答案 0 :(得分:3)
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