Delete Item In Array If Conditions Met

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

···

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

Chris Kalaboukis wrote:

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!

···

--
Posted via http://www.ruby-forum.com/\.

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' }

···

On Thu, Feb 11, 2010 at 17:49, Chris Kalaboukis <thinkfuture@gmail.com> wrote:

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
--
Posted via http://www.ruby-forum.com/\.

Chris Kalaboukis wrote:

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

···

--
Posted via http://www.ruby-forum.com/\.

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

···

On Thu, Feb 11, 2010 at 3:55 PM, Siep Korteling <s.korteling@gmail.com>wrote:

Chris Kalaboukis wrote:
> 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

--
Posted via http://www.ruby-forum.com/\.

--
Tony Arcieri
Medioh! A Kudelski Brand

Mat Brown wrote:

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!

···

--
Posted via http://www.ruby-forum.com/\.

Chris Kalaboukis wrote:

Mat Brown wrote:

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!

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.

···

--
Posted via http://www.ruby-forum.com/\.