Forum: Ruby Re: 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.
87e9a89c53ccf984db792113471c2171?d=identicon&s=25 Kroeger, Simon (ext) (Guest)
on 2006-03-13 14:37
(Received via mailing list)
> "BOCA RATON".replace "Boca Raton" # :-P
>
> Probably
>
> "BOCA RATON".split(/\b/).map{|s| s.capitalize }.join
>
> It is quick to write, at least...
>
> HTH

"BOCA RATON".gsub(/\w+/){|w| w.capitalize}

is even shorter :)

cheers

Simon
1a4f96b06801ac0e30fda3bb0f795313?d=identicon&s=25 Pete Palmer (ppalmer)
on 2006-03-13 14:37
(Received via mailing list)
Hi,

I'm new to Ruby but can't you just use :

"BOCA RATON".capitalize

?


Cheers,


Pete
1a4f96b06801ac0e30fda3bb0f795313?d=identicon&s=25 Pete Palmer (ppalmer)
on 2006-03-13 14:46
(Received via mailing list)
Answer : No you can't. Please ignore me ;)
Ea9c961a40599c37b750dc36df620623?d=identicon&s=25 Tassilo Horn (Guest)
on 2006-03-13 15:20
(Received via mailing list)
Peter Palmer <ppalmer@naniteservices.co.uk> writes:

> I'm new to Ruby but can't you just use :
>
> "BOCA RATON".capitalize

irb(main):001:0> "BOCA RATON".capitalize
=> "Boca raton"

Regards,
Tassilo
Bc6d88907ce09158581fbb9b469a35a3?d=identicon&s=25 James Britt (Guest)
on 2006-03-13 15:29
(Received via mailing list)
Peter Palmer wrote:
> Answer : No you can't. Please ignore me ;)

Do we ignore the message asking us to ignore you?

:)

Anyway, you were on the right track, but the string needs to be chunked
on white space, then regrouped.

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

If one tends to do this a lot, it can be added as an instance method to
String:

class String
   def titlize
     self.split( /\s/).map{ |w| w.capitalize }.join( ' ' )
   end
end

"BOCA RATON".titlize

--
James Britt

?Design depends largely on constraints.?
  ? Charles Eames
F5b3c1ebfb2e9fc5f67bb48b119f6054?d=identicon&s=25 Randy Kramer (Guest)
on 2006-03-13 19:02
(Received via mailing list)
On Monday 13 March 2006 08:36 am, Peter Palmer wrote:
> Hi,
>
> I'm new to Ruby but can't you just use :
>
> "BOCA RATON".capitalize

Nope!  (That gives => "Boca raton").

And, even as a Ruby newbie, I determined in seconds.

The real point of my post is to say that, while I'm learning Ruby, I
have IRB
open in a konsole right next to kmail--it's very nice to quickly plug a
piece
of code in there and do some testing (confirm it works, take bits off
the end
to see intermediate results, etc.).

regards,
Randy Kramer
This topic is locked and can not be replied to.