ruby - Take a string, capitalize only words with more than three characters unless it is in the beginning -
when enter input, "war , peace", want, "war , peace". idea take string , make appear headline or movie title. capitalize words in string, unless word equal or less 3 , not in beginning.
when run code:
def titleize(string) string.split(" ").take(1).join.capitalize string.split(" ").map |x| x.capitalize! if x.length > 3 end.join(" ") end
it returns " peace".
try this:
title = 'war , peace' def titleize(input) input.split(' ').map.with_index { |word, index| index == 0 || word.length > 3 ? word.capitalize : word }.join(' ') end titleize title => "war , peace"
Comments
Post a Comment