Project

General

Profile

Actions

Feature #4822

closed

String#capitalize improvements

Added by yeban (Anurag Priyam) almost 13 years ago. Updated about 12 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:36714]

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.

Updated by yeban (Anurag Priyam) almost 13 years ago

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

Updated by yeban (Anurag Priyam) almost 13 years ago

You should use String#titleize provided by ActiveSupport.
http://as.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html#M000381

But that capitalizes all the words in a string, no?. I am talking
about capitalizing the first letter of each sentence.

--
Anurag Priyam
http://about.me/yeban/

Updated by naruse (Yui NARUSE) almost 13 years ago

Anurag Priyam wrote:

You should use String#titleize provided by ActiveSupport.
http://as.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html#M000381

But that capitalizes all the words in a string, no?. I am talking
about capitalizing the first letter of each sentence.

Hmm, how about this?
str.gsub(/\w.*?.!?/){|c|c.capitalize}

Of course, this is wrong for example "iPhone is designed by Apple in California."

Updated by yeban (Anurag Priyam) almost 13 years ago

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/

Updated by naruse (Yui NARUSE) almost 13 years ago

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.

Updated by duerst (Martin Dürst) about 12 years ago

  • 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.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0