Henrik Ormåsen wrote:
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: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