gpt4 book ai didi

ruby - 创建一个 titleize 方法,排除 "little words"。

转载 作者:太空宇宙 更新时间:2023-11-03 18:23:59 25 4
gpt4 key购买 nike

我正在制作一种方法来将我的输入大写,除了 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com