Deleting object inn array, if

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 :-)).

On 12/4/05, Henrik Ormåsen [email protected] wrote:

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

How can I do this?

how about ?

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

On 12/4/05, Simon S. [email protected] wrote:

On 12/4/05, Henrik Ormåsen [email protected] wrote:

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

How can I do this?

how about ?

[1, 2, 3, 4, 5].delete_if {|i| i==3} #=> [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?

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

On Dec 4, 2005, at 5:06 PM, Henrik Ormåsen wrote:

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 = Article.new
b = Article.new
c = Article.new

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

or is your database something like:

database = [a, b, c]

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

Could you clarify?

Henrik Ormåsen wrote:

@body = body

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

Henrik Ormåsen wrote:

@body = body

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:oxb7ca1120 @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