Ruby: Titleize: How do I ignore smaller words like 'and', 'the', 'or, etc

前端 未结 9 1672
猫巷女王i
猫巷女王i 2020-12-31 13:13
def titleize(string)
  string.split(\" \").map {|word| word.capitalize}.join(\" \")
end

This titleizes every single word, but how do I capture cert

9条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-31 14:09

    This is pretty straightforward, just add a condition when you call captalize:

    $nocaps = ['Jack', 'Jill']
    
    def titleize(string)
      string.split(" ").map {|word| word.capitalize unless $nocaps.include?(word)}.join(" ")
    end
    

    The global variables are contrived for this example, it would probably be an instance variable in your real application.

提交回复
热议问题