Delete Item In Array If Conditions Met

C

Chris Kalaboukis

Hi all: Ruby newbie here. Been trying to get this code to work but am
having a tough time of it. Can anyone else please help me out?

I've read from a db into an array, and would like to go through the
array and remove elements which match a specific criteria. Here is the
code that I'm using:


# load array
@array = Capture.find:)all)

# iterate through array

for x in @array
x.reject!{|x| x.text =~ "something"}
end
 
C

Chris Kalaboukis

Chris said:
Hi all: Ruby newbie here. Been trying to get this code to work but am
having a tough time of it. Can anyone else please help me out?

I've read from a db into an array, and would like to go through the
array and remove elements which match a specific criteria. Here is the
code that I'm using:


# load array
@array = Capture.find:)all)

# iterate through array

for x in @array
x.reject!{|x| x.text =~ "something"}
end

Sorry: forgot to mention that this breaks with an undefined method
reject!

Thanks in advance!
 
M

Mat Brown

The #reject! method is an iterator; so is the for loop (effectively).
So you're iterating over the array, and then trying to iterate over
individual members of the array. Here's what you want:

@array =3D Capture.find:)all)
@array.reject! { |item| item.text =3D~ 'something' }
 
S

Siep Korteling

Chris said:
Hi all: Ruby newbie here. Been trying to get this code to work but am
having a tough time of it. Can anyone else please help me out?

I've read from a db into an array, and would like to go through the
array and remove elements which match a specific criteria. Here is the
code that I'm using:


# load array
@array = Capture.find:)all)

# iterate through array

for x in @array
x.reject!{|x| x.text =~ "something"}
end

reject! does the iterating for you, so you can (must) leave out the for
loop.

hth,

Siep
 
T

Tony Arcieri

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

In addition to reject! there's also Array#delete_if
 
C

Chris Kalaboukis

Mat said:
The #reject! method is an iterator; so is the for loop (effectively).
So you're iterating over the array, and then trying to iterate over
individual members of the array. Here's what you want:

@array = Capture.find:)all)
@array.reject! { |item| item.text =~ 'something' }

Thanks guys that did it!
 
A

Albert Schlef

Chris said:
Thanks guys that did it!

You've better ask your question in the Rails forum. You're using some
ORM (e.g. AcriveRecord, DataMapper, etc.) and need to ask the people who
use it.

Instead of loading all the objects from the database it's more efficient
to pass find() the criterion.
 

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,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top