Feature #4822
closed
String#capitalize improvements
Added by yeban (Anurag Priyam) over 13 years ago.
Updated over 12 years ago.
Description
I think it would be helpful if String#capitalize
could capitalize sentences, and not just the first letter of a string. We could optionally pass a regexp to identify sentence boundaries. If we don't pass this parameter capitalize
behaves as before.
s = "hey all! wassup? i am good."
current capitalize¶
s.capitalize #=> "Hey all! wassup? i am good."
new capitalize¶
s.capitalize #=> "Hey all! wassup? i am good."
s.capitalize(/[?!.] /) #=> "Hey all! Wassup? I am good."
I am not sure what would it take to implement this.
I am not sure what would it take to implement this.
I hacked up a quick, and dirty implementation in Ruby.
class String
alias _capitalize capitalize
def capitalize(regexp=nil)
return _capitalize unless regexp
return split(regexp).zip(scan(regexp)).map(&:join).map(&:_capitalize).join
end
end
Hmm, how about this?
str.gsub(/\w.*?.!?/){|c|c.capitalize}
This solves my problem better than what I was doing. Thanks. Do you
think that other's could also benefit from such an extension to
capitalize
, or is it better done problem specific?
Of course, this is wrong for example "iPhone is designed by Apple in California."
Neither does mine :-|.
--
Anurag Priyam
http://about.me/yeban/
Anurag Priyam wrote:
Hmm, how about this?
str.gsub(/\w.*?.!?/){|c|c.capitalize}
This solves my problem better than what I was doing. Thanks. Do you
think that other's could also benefit from such an extension to
capitalize
, or is it better done problem specific?
As my example, this can't be perfect.
Such function should be provided by third party, like gems.
- Status changed from Open to Rejected
yeban (Anurag Priyam) wrote:
Hmm, how about this?
str.gsub(/\w.*?.!?/){|c|c.capitalize}
This solves my problem better than what I was doing. Thanks. Do you
think that other's could also benefit from such an extension to
capitalize
, or is it better done problem specific?
We have discussed this issue at today's developers' meeting in Akihabara.
This kind of processing is not only dependent on language, but even for a specific language needs a lot of information to do a reasonable job. To do it perfectly seems to require human intervention. Therefore, we think it is better if requirements like this are addressed with problem-specific solutions. We are therefore rejecting this issue.
Also available in: Atom
PDF
Like0
Like0Like0Like0Like0Like0Like0Like0