我正在制作一种方法来大写我的输入,除了 a、an 和...等任何单词。
def titleize(string_to_titleize)
string_to_titleize.split(' ').map { |words| words.capitalize }.join(' ')
end
我知道有一些 gem 可以做到这一点。我无法掌握如何手动完成。我假设创建一个不大写的单词列表。然后,将他们排除在外。
最佳答案
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
关于ruby - 创建一个 titleize 方法,不包括 "little words"。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13813874/