[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Iterating through a string

Jody Glidden

9/21/2008 9:03:00 PM

If I try the following...

"I love guitar playing".split {|s| s.capitalize}.join(" ")

I would expect it to capitalize the first letter of each word but it
doesn't seem to work.

Any hints on what I'm doing incorrectly?
--
Posted via http://www.ruby-....

9 Answers

Stefano Crocco

9/21/2008 9:06:00 PM

0

Alle Sunday 21 September 2008, Jody Glidden ha scritto:
> If I try the following...
>
> "I love guitar playing".split {|s| s.capitalize}.join(" ")
>
> I would expect it to capitalize the first letter of each word but it
> doesn't seem to work.
>
> Any hints on what I'm doing incorrectly?


String#split doesn't take a block, so that the block you pass to it is
ignored. What you wrote is equivalent to

"I love guitar playing".split.join(" ")

To do what you want, you need to call map on the array returned by
String#split:

"I love guitar playing".split.map{|s| s.capitalize}.join(" ")

I hope this helps

Stefano

Jody Glidden

9/21/2008 9:10:00 PM

0

Stefano Crocco wrote:
> Alle Sunday 21 September 2008, Jody Glidden ha scritto:
>> If I try the following...
>>
>> "I love guitar playing".split {|s| s.capitalize}.join(" ")
>>
>> I would expect it to capitalize the first letter of each word but it
>> doesn't seem to work.
>>
>> Any hints on what I'm doing incorrectly?
>
>
> String#split doesn't take a block, so that the block you pass to it is
> ignored. What you wrote is equivalent to
>
> "I love guitar playing".split.join(" ")
>
> To do what you want, you need to call map on the array returned by
> String#split:
>
> "I love guitar playing".split.map{|s| s.capitalize}.join(" ")
>
> I hope this helps
>
> Stefano

Ahh, I understand. I must have missed the map/collect functionality.

Thanks.
--
Posted via http://www.ruby-....

Siep Korteling

9/21/2008 9:36:00 PM

0

Ruby Fan wrote:
> Stefano Crocco wrote:
>> Alle Sunday 21 September 2008, Jody Glidden ha scritto:
>>> If I try the following...
>>>
>>> "I love guitar playing".split {|s| s.capitalize}.join(" ")
>>>
>>> I would expect it to capitalize the first letter of each word but it
>>> doesn't seem to work.
>>>
>>> Any hints on what I'm doing incorrectly?
>>
>>
>> String#split doesn't take a block, so that the block you pass to it is
>> ignored. What you wrote is equivalent to
>>
>> "I love guitar playing".split.join(" ")
>>
>> To do what you want, you need to call map on the array returned by
>> String#split:
>>
>> "I love guitar playing".split.map{|s| s.capitalize}.join(" ")
>>
>> I hope this helps
>>
>> Stefano
>
> Ahh, I understand. I must have missed the map/collect functionality.
>
> Thanks.

"I love guitar playing".split.each{|s| s.capitalize}.join(" ")

does not work because #capitalize does not modify the original; it gives
you a copy. Map/collect really collects these copies, #each does not.
However

"I love guitar playing".split.each{|s| s.capitalize!}.join(" ")

Does work, because the self-modifying #capitalize! is available.
Personally I'm starting to prefer #map , also because of this:

a = (1..9).map{|n| n*n}

#versus my old way

a = [] #annoying
(1..9).each{|n| a<<n*n}

Regards,

Siep
--
Posted via http://www.ruby-....

William James

9/21/2008 10:45:00 PM

0



Jody Glidden wrote:
> If I try the following...
>
> "I love guitar playing".split {|s| s.capitalize}.join(" ")
>
> I would expect it to capitalize the first letter of each word but it
> doesn't seem to work.


"I love guitar playing".gsub( /\S+/ ){|s| s.capitalize}
==>"I Love Guitar Playing"

Randy Kramer

9/21/2008 11:08:00 PM

0

On Sunday 21 September 2008 06:37 pm, William James wrote:
> Jody Glidden wrote:
> > If I try the following...
> >
> > "I love guitar playing".split {|s| s.capitalize}.join(" ")
> >
> > I would expect it to capitalize the first letter of each word but it
> > doesn't seem to work.

Just for kicks, I managed to modify your approach to make it work (at
least in irb):

"I love guitar playing".split.each {|s| s.capitalize!}.join(" ")

So, it took the addition of each to cause the block to actually do
something, and switching from capitalize to capitalize! (Which, iiuc,
causes the original object to be changed, rather than creating a new
changed object.

Try substituting things like puts "test" in the body of the block to
experiment.

Randy Kramer
--
I didn't have time to write a short letter, so I created a video
instead.--with apologies to Cicero, et.al.

botp

9/22/2008 12:33:00 AM

0

On Mon, Sep 22, 2008 at 6:37 AM, William James <w_a_x_man@yahoo.com> wrote:
> "I love guitar playing".gsub( /\S+/ ){|s| s.capitalize}
> ==>"I Love Guitar Playing"

it would be great if #split accept a block, too

Stephen Celis

9/22/2008 1:35:00 AM

0

On Sep 21, 2008, at 7:32 PM, botp wrote:

> it would be great if #split accept a block, too


When ".map" is only another 4 characters, is it worth the pollution?

It's an easy monkey-patch, though.

Stephen

Peña, Botp

9/22/2008 1:44:00 AM

0

From: Stephen Celis [mailto:stephen.celis@gmail.com]=20
# When ".map" is only another 4 characters, is it worth the pollution?

that's a 2 pass

Brian Candler

9/22/2008 7:50:00 AM

0

Siep Korteling wrote:
> a = (1..9).map{|n| n*n}
>
> #versus my old way
>
> a = [] #annoying
> (1..9).each{|n| a<<n*n}

FYI, there's another pattern for "building the result" without the
annoying assignment:

a = (1..9).inject([]) { |acc,n| acc << n*n }

Or in a more functional style (without any array mutation)

a = (1..9).inject([]) { |acc,n| acc + [n*n] }

However in this case, map is perfectly appropriate. inject is useful
when you're building something that isn't an Array - for example a
String or a Hash.
--
Posted via http://www.ruby-....