[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Remove from an array

Nicolas Benady

8/20/2006 7:49:00 AM

Hello,
I have an array, let's name it items[]
I want to remove an object in a elegant way
items.remove(3) does not work
How should I do ?
Thanks

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

5 Answers

Paul Lutus

8/20/2006 8:12:00 AM

0

Nicolas Benady wrote:

> Hello,
> I have an array, let's name it items[]
> I want to remove an object in a elegant way
> items.remove(3) does not work
> How should I do ?
> Thanks
>

items.delete_at(3)

Just like in the documentation.

--
Paul Lutus
http://www.ara...

Hal E. Fulton

8/20/2006 8:17:00 AM

0

Paul Lutus wrote:
> Nicolas Benady wrote:
>
>
>>Hello,
>>I have an array, let's name it items[]
>>I want to remove an object in a elegant way
>>items.remove(3) does not work
>>How should I do ?
>>Thanks
>>
>
>
> items.delete_at(3)
>
> Just like in the documentation.
>

Did he want to remove the element at index 3,
or remove instances of 3?

Hal


Paul Lutus

8/20/2006 10:02:00 AM

0

Hal Fulton wrote:

> Paul Lutus wrote:
>> Nicolas Benady wrote:
>>
>>
>>>Hello,
>>>I have an array, let's name it items[]
>>>I want to remove an object in a elegant way
>>>items.remove(3) does not work
>>>How should I do ?
>>>Thanks
>>>
>>
>>
>> items.delete_at(3)
>>
>> Just like in the documentation.
>>
>
> Did he want to remove the element at index 3,
> or remove instances of 3?

I suppose I should have asked that before replying. :)

If your theory is correct, then:

items.delete_if { |x| x == 3 }

Removes all items matching "3".

--
Paul Lutus
http://www.ara...

Jeff Schwab

8/20/2006 3:01:00 PM

0

Paul Lutus wrote:
> Hal Fulton wrote:
>
>> Paul Lutus wrote:
>>> Nicolas Benady wrote:
>>>
>>>
>>>> Hello,
>>>> I have an array, let's name it items[]
>>>> I want to remove an object in a elegant way
>>>> items.remove(3) does not work
>>>> How should I do ?
>>>> Thanks
>>>>
>>>
>>> items.delete_at(3)
>>>
>>> Just like in the documentation.
>>>
>> Did he want to remove the element at index 3,
>> or remove instances of 3?
>
> I suppose I should have asked that before replying. :)
>
> If your theory is correct, then:
>
> items.delete_if { |x| x == 3 }
>
> Removes all items matching "3".

items.delete(3)

Paul Lutus

8/20/2006 3:48:00 PM

0

Jeffrey Schwab wrote:

/ ...

>> If your theory is correct, then:
>>
>> items.delete_if { |x| x == 3 }
>>
>> Removes all items matching "3".
>
> items.delete(3)

Yes, better. I overlooked this approach.

--
Paul Lutus
http://www.ara...