How to remove empty element in an array

Discussion in 'Ruby' started by Li Chen, Oct 23, 2006.

  1. Li Chen

    Li Chen Guest

    Hi all,

    I have an array of [1,2,''] I want change it to [1,2]. I check the
    document about Array but I cant't find a way to remove the empty
    element. Any comments?


    Li Chen, Oct 23, 2006
    1. Advertisements


    You should really get hold of your copy of the Pickaxe.


    Robert Klemme, Oct 23, 2006
    1. Advertisements

  3. It's "[1, 2, <empty-string>]"? If so, then it's not an empty element.
    If you have [1, 2, nil] then you can just use Array#compact or
    Array#compact!. Otherwise, look at #delete_if.
    Austin Ziegler, Oct 23, 2006
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.