Deleting object inn array, if...

  • Thread starter Henrik Ormåsen
  • Start date
H

Henrik Ormåsen

I have an array witch contains the id of living objects. I want to
delete objects from the array, if the objects contains an variable ==
"other_var".

I was thinking of something like the following thing:

FAKE_DATABASE.delete_if {|x| x.var_of_x == @body }

I thought this would bee logical, but it doesn't seem like Ruby give
me right here.

How can I do this?

Regards Henrik
(PS: Pleas don't kill me if I did something stupid here, I'm newbie on
Ruby :)).
 
S

Simon Strandgaard

FAKE_DATABASE.delete_if {|x| x.var_of_x =3D=3D @body } [snip]

How can I do this?

how about ?

[1, 2, 3, 4, 5].delete_if {|i| i=3D=3D3} #=3D> [1, 2, 4, 5]
 
S

Simon Strandgaard

FAKE_DATABASE.delete_if {|x| x.var_of_x =3D=3D @body } [snip]

How can I do this?

how about ?

[1, 2, 3, 4, 5].delete_if {|i| i=3D=3D3} #=3D> [1, 2, 4, 5]

Argh.. realized that you wanted x.var_of_x to be deleted.
I don't understand what you want.. can you elaborate?
 
G

gwtmp01

I have an array witch contains the id of living objects.

Do you mean that you are actually storing the object_id of a Ruby
object? Something like:

class Article
attr_accessor :body
end

a =3D Article.new
b =3D Article.new
c =3D Article.new

database =3D [a.object_id, b.object_id, c.object_id]

or is your database something like:

database =3D [a, b, c]

or are you talking about primary keys from a relational database?

Could you clarify?=
 
H

Henrik Ormåsen

Thanks for the fast replay's, I'm impressed :).

Her is my DB thing:

class Item
attr_reader :body

FAKE_DATABASE = []

def initialize(body)
@body = body
end
def add
FAKE_DATABASE.unshift(self)
end
def self.find_recent
FAKE_DATABASE
end
def del
FAKE_DATABASE.delete_if {|x| x.body == @body }
end
end


- Henrik
 
T

Todd

Henrik said:
Thanks for the fast replay's, I'm impressed :).

Her is my DB thing:

class Item
attr_reader :body

FAKE_DATABASE = []

def initialize(body)
@body = body
end
def add
FAKE_DATABASE.unshift(self)
end
def self.find_recent
FAKE_DATABASE
end
def del
FAKE_DATABASE.delete_if {|x| x.body == @body }
end
end


- Henrik

Sounds maybe you are trying to do something like:

<irb(main):001:0> class Item
<irb(main):002:1> attr_reader :body
<irb(main):003:1> @@db = []
<irb(main):004:1> def initialize( body ) @body=body end
<irb(main):005:1> def add() unshift.self end
<irb(main):006:1> def recent() @@db end
<irb(main):007:1> def del @@db.delete_if { |x| x.body==@body }
<irb(main):008:1> end
=> nil

<irb(main):009:0> a = Item.new "a"
=> #<Item:eek:xb7ca1120 @body="a">

<irb(main):010:0> b = Item.new "b"
=> #<Item:0xb7c9c314 @body="b">

<irb(main):011:0> a.add
=> [#<Item:0xb7ca1120 @body="a">]

<irb(main):012:0> b.add
=> [#<Item:0xb7c9c314 @body="b">, #<Item:0xb7ca1120 @body="a">]

<irb(main):013:0> a.recent
=> same thing as above

<irb(main):014:0> b.recent
=> same thing as above

<irb(main):015:0> a.add
=> [#<item:0xb7ca1120 @body="a", #<Item:0xb7c9c314 @body="b">,
#<Item:0xb7ca1120 @body="a">]
note there are 2 "a" objects in the same db, you probably don't want
this, so will have to change your add method accordingly

<irb(main):016:0> a.recent == b.recent
=> true

<irb(main):017:0> a.del
=> [#<item:0xb7ca9c314 @body="b">]

<irb(main):018:0> b.del
=> []

<irb(main):019:0> a.recent == b.recent
=> true

At least that's the direction you look like you are headed.

hth,
Todd
 
R

Robert Klemme

Henrik said:
Thanks for the fast replay's, I'm impressed :).

Her is my DB thing:

class Item
attr_reader :body

FAKE_DATABASE = []

def initialize(body)
@body = body
end
def add
FAKE_DATABASE.unshift(self)
end
def self.find_recent
FAKE_DATABASE
end
def del
FAKE_DATABASE.delete_if {|x| x.body == @body }
end
end


- Henrik

What's your problem? Deletion works ok:

irb(main):020:0* a=Item.new "a"
=> #<Item:0x10192810 @body="a">
irb(main):021:0> a.add
=> [#<Item:0x10192810 @body="a">]
irb(main):022:0> Item.find_recent
=> [#<Item:0x10192810 @body="a">]
irb(main):023:0> a.del
=> []
irb(main):024:0> Item.find_recent
=> []
irb(main):025:0>

Kind regards

robert
 

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

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top