# Simple, simple array question

**URL:** <https://www.ruby-forum.com/t/simple-simple-array-question/134164>\
**Category:** Ruby\
**Created:** [April 8, 2008, 3:34pm UTC](https://www.ruby-forum.com/t/simple-simple-array-question/134164 "2008-04-08T15:34:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [April 8, 2008, 3:34pm UTC](https://www.ruby-forum.com/t/simple-simple-array-question/134164/1 "2008-04-08T15:34:54Z")

</div>

Hi,  
I need to cap-and-lowercase words in strings of XML data. Why is this  
happening in this test?

stuff = [“This”, “is”, “a”, “test”]  
stuff.collect { |x| puts x.capitalize! }

I get:

nil  
Is  
A  
Test

Program exited with code 0

What’s with the “nil?”

Thanks,  
Peter

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [April 8, 2008, 3:42pm UTC](https://www.ruby-forum.com/t/simple-simple-array-question/134164/2 "2008-04-08T15:42:44Z")

</div>

I’m hardly any good at Ruby yet, but isn’t “Test” already capitalized?  
Maybe  
that’s why it’s nil?

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [April 8, 2008, 3:43pm UTC](https://www.ruby-forum.com/t/simple-simple-array-question/134164/3 "2008-04-08T15:43:57Z")

</div>

Hi,

I’m so tired, so here’s a succinct answer:

`capitalize!' (with the exclamation mark on the end) actually changes`x’  
itself:

> > a = “boo”  
> > =\> “boo”
> 
> > a.capitalize!  
> > =\> “Boo”
> 
> > a  
> > =\> “Boo”
> 
> >

Notice how `a’ has a new value. Now, what if we try to capitalize!  
again?

> > a.capitalize!  
> > =\> nil
> 
> > a  
> > =\> “Boo”
> 
> >

It returns nil. `a' is still "Boo" - the thing is, capitalize! returns nil because`a’ didn’t change. With your example, `This' is already capitalized, so no change occurred. What you really want is`x.capitalize’ - it just  
returns the value of x capitalized, whatever that is:

> > a = “boo”  
> > =\> “boo”
> 
> > a.capitalize  
> > =\> “Boo”
> 
> > a  
> > =\> “boo”
> 
> >

It also doesn’t change `x’ - not that it would matter much in your given  
example.

Also, though it’s probably just your test, but note that you’re not  
saving  
the result of `collect’ anywhere. As an example:

> > a = %w(This is a test.)  
> > =\> [“This”, “is”, “a”, “test.”]
> 
> > a = %w(This is a test)  
> > =\> [“This”, “is”, “a”, “test”]
> 
> > a.collect {|x| x.capitalize}  
> > =\> [“This”, “Is”, “A”, “Test”]

We get this answer, but …

> > a  
> > =\> [“This”, “is”, “a”, “test”]

It’s still lowercase! So we can use the collect! method to alter the  
original:

> > a.collect! {|x| x.capitalize}  
> > =\> [“This”, “Is”, “A”, “Test”]
> 
> > a  
> > =\> [“This”, “Is”, “A”, “Test”]
> 
> >

Or you could assign the result of `collect’.

And I’m out for the night.

HTH,  
Arlen

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [April 8, 2008, 3:44pm UTC](https://www.ruby-forum.com/t/simple-simple-array-question/134164/4 "2008-04-08T15:44:30Z")

</div>

Peter B. wrote:

> What’s with the “nil?”

capitalize! returns nil when the string is already capitalized. Use  
capitalize instead. Also, you don’t need the puts inside the block.

stuff.collect { |x| x.capitalize }  
puts stuff

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [April 8, 2008, 3:46pm UTC](https://www.ruby-forum.com/t/simple-simple-array-question/134164/5 "2008-04-08T15:46:21Z")

</div>

Sorry, I meant “This” already capitalized. Try it lowercase?

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [April 8, 2008, 3:45pm UTC](https://www.ruby-forum.com/t/simple-simple-array-question/134164/6 "2008-04-08T15:45:10Z")

</div>

Hi,

And excuse my top-posting, I really am tired!

Cheers,  
Arlen

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [April 8, 2008, 3:49pm UTC](https://www.ruby-forum.com/t/simple-simple-array-question/134164/7 "2008-04-08T15:49:52Z")

</div>

Peter B. wrote:

> Is  
> A  
> Test
> 
> Program exited with code 0
> 
> What’s with the “nil?”
> 
> Thanks,  
> Peter

This is because you are using collect where, I suspect, you want to use  
each or perhaps drop the puts.

irb(main):004:0\> stuff = [“This”, “is”, “a”, “test”]  
=\> [“This”, “is”, “a”, “test”]  
irb(main):005:0\> stuff.collect{|x| x.capitalize }  
=\> [“This”, “Is”, “A”, “Test”]  
irb(main):006:0\>

Maybe that is what you want.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [April 8, 2008, 3:54pm UTC](https://www.ruby-forum.com/t/simple-simple-array-question/134164/8 "2008-04-08T15:54:52Z")

</div>

Peter H. wrote:

> Peter B. wrote:
> 
> > Is  
> > A  
> > Test
> > 
> > Program exited with code 0
> > 
> > What’s with the “nil?”
> > 
> > Thanks,  
> > Peter
> 
> This is because you are using collect where, I suspect, you want to use  
> each or perhaps drop the puts.
> 
> irb(main):004:0\> stuff = [“This”, “is”, “a”, “test”]  
> =\> [“This”, “is”, “a”, “test”]  
> irb(main):005:0\> stuff.collect{|x| x.capitalize }  
> =\> [“This”, “Is”, “A”, “Test”]  
> irb(main):006:0\>
> 
> Maybe that is what you want.

Thanks all you guys. Yeh, I feel kinda’ dumb. That first word was  
already capitalized. Duh.

Cheers,  
-Peter
