2012-12-11 2 views
1

나는 a, an, ... 등과 같은 단어를 제외하고 입력을 대문자로 사용하는 방법을 만들고있다."작은 단어"를 제외하는 titleize 메서드를 만듭니다.

def titleize(string_to_titleize) 
    string_to_titleize.split(' ').map { |words| words.capitalize }.join(' ') 
end 

내가 알고있는 일이 있습니다. 나는 그것을 수동으로하는 방법을 이해할 수 없다. 대문자로 표기하지 않는 단어의 목록을 만드는 것으로 가정합니다. 그런 다음 다른 사람을 제외시킵니다.

+2

중복? http://stackoverflow.com/questions/13117340/using-title-case-with-ruby-1-8-7/13117575#13117575 – oldergod

답변

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 
+0

이것은 매우 도움이되었습니다. 도와 줘서 고마워. – Feocco

관련 문제