delete in Array (if exist) and count

J

Josselin

I am trying to delete an element from an array if it exists and count
the number of item afterwards

I wrote

anArray = [1,2,3,4,5,6,7,8,9]
anArray.size
=> 9
anArray.map {|e| e if e != 4}.compact
=> [1, 2, 3, 5, 6, 7, 8, 9]
anArray.map {|e| e if e != 4}.compact.size
=> 8


is ther a better way to do it... it it's enough ? (learning always how
to write better code..)

thanks

joss
 
T

Tim Hunter

Josselin said:
I am trying to delete an element from an array if it exists and count
the number of item afterwards
How about using Array#delete_if?
 
T

Todd Benson

I am trying to delete an element from an array if it exists and count
the number of item afterwards

I wrote

anArray = [1,2,3,4,5,6,7,8,9]
anArray.size
=> 9
anArray.map {|e| e if e != 4}.compact
=> [1, 2, 3, 5, 6, 7, 8, 9]
anArray.map {|e| e if e != 4}.compact.size
=> 8


is ther a better way to do it... it it's enough ? (learning always how
to write better code..)

Array#delete works also with no need for a block (i.e. you're only
checking for equality)

anArray = [1,2,3,4,5,6]
p anArray.delete(4)
p anArray.size
 
J

Josselin

I am trying to delete an element from an array if it exists and count
the number of item afterwards

I wrote

anArray = [1,2,3,4,5,6,7,8,9]
anArray.size
=> 9
anArray.map {|e| e if e != 4}.compact
=> [1, 2, 3, 5, 6, 7, 8, 9]
anArray.map {|e| e if e != 4}.compact.size
=> 8


is ther a better way to do it... it it's enough ? (learning always how
to write better code..)

Array#delete works also with no need for a block (i.e. you're only
checking for equality)

anArray = [1,2,3,4,5,6]
p anArray.delete(4)
p anArray.size

I checked
anArray.delete(0)
=> nil
then
anArray.delete(0).size
NoMethodError: undefined method `size' for nil:NilClass

as I tried to write it in one line....

thanks

joss
 
J

Josselin

How about using Array#delete_if?

that's exact§ly what I want... I read the API doc, I look into the
delete.. but why I stop before the delete_if ?????
thanks a lot ..

Joss
 
T

Todd Benson

I am trying to delete an element from an array if it exists and count
the number of item afterwards

I wrote

anArray = [1,2,3,4,5,6,7,8,9]
anArray.size
=> 9
anArray.map {|e| e if e != 4}.compact
=> [1, 2, 3, 5, 6, 7, 8, 9]
anArray.map {|e| e if e != 4}.compact.size
=> 8


is ther a better way to do it... it it's enough ? (learning always how
to write better code..)

Array#delete works also with no need for a block (i.e. you're only
checking for equality)

anArray = [1,2,3,4,5,6]
p anArray.delete(4)
p anArray.size

I checked
anArray.delete(0)
=> nil
then
anArray.delete(0).size
NoMethodError: undefined method `size' for nil:NilClass

as I tried to write it in one line....

thanks

joss

That's because Array#delete returns the deletion, not the Array
itself, so, if it has to be on one line, then:

(anArray.delete(4); a).size

or

anArray.delete_if {|i| i == 4}.size

or I prefer

anArray.reject! {|i| i == 4}.size

#delete, #delete_if, and #reject! all change the array while #reject does not
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top