simple, simple array question

Discussion in 'Ruby' started by Peter Bailey, Apr 8, 2008.

  1. Peter Bailey

    Peter Bailey Guest

    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
    --
    Posted via http://www.ruby-forum.com/.
     
    Peter Bailey, Apr 8, 2008
    #1
    1. Advertising

  2. Peter Bailey

    Sam Rudd Guest

    [Note: parts of this message were removed to make it a legal post.]

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

    On Tue, Apr 8, 2008 at 2:35 PM, Peter Bailey <> wrote:

    > 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
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
     
    Sam Rudd, Apr 8, 2008
    #2
    1. Advertising

  3. Peter Bailey

    Arlen Cuss Guest

    [Note: parts of this message were removed to make it a legal post.]

    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

    On Tue, Apr 8, 2008 at 11:35 PM, Peter Bailey <> wrote:

    > 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
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
     
    Arlen Cuss, Apr 8, 2008
    #3
  4. Peter Bailey

    Arlen Cuss Guest

    [Note: parts of this message were removed to make it a legal post.]

    Hi,

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

    Cheers,
    Arlen
     
    Arlen Cuss, Apr 8, 2008
    #4
  5. Peter Bailey

    Sam Rudd Guest

    [Note: parts of this message were removed to make it a legal post.]

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

    On Tue, Apr 8, 2008 at 2:41 PM, Sam Rudd <> wrote:

    > I'm hardly any good at Ruby yet, but isn't "Test" already capitalized?
    > Maybe that's why it's nil?
    >
    > On Tue, Apr 8, 2008 at 2:35 PM, Peter Bailey <> wrote:
    >
    > > 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
    > > --
    > > Posted via http://www.ruby-forum.com/.
    > >
    > >

    >
     
    Sam Rudd, Apr 8, 2008
    #5
  6. Peter Bailey

    Tim Hunter Guest

    Peter Bailey 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
    --
    Posted via http://www.ruby-forum.com/.
     
    Tim Hunter, Apr 8, 2008
    #6
  7. Peter Bailey wrote:
    > 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
    >

    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.
     
    Peter Hickman, Apr 8, 2008
    #7
  8. Peter Bailey

    Peter Bailey Guest

    Peter Hickman wrote:
    > Peter Bailey 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

    --
    Posted via http://www.ruby-forum.com/.
     
    Peter Bailey, Apr 8, 2008
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joseph Turian
    Replies:
    11
    Views:
    534
    Joseph Turian
    Jan 19, 2005
  2. Fabio
    Replies:
    0
    Views:
    325
    Fabio
    Nov 5, 2008
  3. Mara Guida

    const and array of array (of array ...)

    Mara Guida, Sep 2, 2009, in forum: C Programming
    Replies:
    3
    Views:
    521
    David RF
    Sep 3, 2009
  4. Tom
    Replies:
    3
    Views:
    234
    salsablr
    Dec 20, 2004
  5. Tuan  Bui
    Replies:
    14
    Views:
    525
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page