Forum: Ruby Quickest way to do this string op.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
unknown (Guest)
on 2006-03-13 15:25
(Received via mailing list)
What's the quickest way to turn "BOCA RATON" into "Boca Raton"?
Carlos (Guest)
on 2006-03-13 15:34
(Received via mailing list)
removed_email_address@domain.invalid wrote:

> What's the quickest way to turn "BOCA RATON" into "Boca Raton"?

"BOCA RATON".replace "Boca Raton" # :-P

Probably

"BOCA RATON".split(/\b/).map{|s| s.capitalize }.join

It is quick to write, at least...

HTH
Tassilo Horn (Guest)
on 2006-03-13 16:20
(Received via mailing list)
removed_email_address@domain.invalid writes:

> What's the quickest way to turn "BOCA RATON" into "Boca Raton"?

irb(main):006:0> str = "BOCA RATON"
irb(main):009:0> str.gsub!(/\w+/) { |s| s.capitalize }
=> "Boca Raton"
irb(main):010:0> str
=> "Boca Raton"

Hope this helps. But I'm not sure if that's the fastest way in meaning
of speed. At least it's the solution which crossed my mind first. ;-)

Regards,
Tassilo
pinki (Guest)
on 2006-03-13 23:20
(Received via mailing list)
I'm also new, but this might be another solution:

"BOCA RATON".split(' ').collect do |name| name.capitalize end.join(' ')

It's not the quickest way though..
This topic is locked and can not be replied to.