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.
F3b786a23390451fa67108781b0c8fed?d=identicon&s=25 unknown (Guest)
on 2006-03-13 14:25
(Received via mailing list)
What's the quickest way to turn "BOCA RATON" into "Boca Raton"?
C2cd72c24873556e278b44b5b3c7ef33?d=identicon&s=25 Carlos (Guest)
on 2006-03-13 14:34
(Received via mailing list)
eastcoastcoder@gmail.com 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
Ea9c961a40599c37b750dc36df620623?d=identicon&s=25 Tassilo Horn (Guest)
on 2006-03-13 15:20
(Received via mailing list)
eastcoastcoder@gmail.com 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
521460594d956095804888d90ea6d164?d=identicon&s=25 pinki (Guest)
on 2006-03-13 22: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.