From: Reginald Tan Date: 2011-04-27T15:08:40+09:00 Subject: Re: including Enumerable in PStore Christopher Dicely wrote in post #995252: > On Tue, Apr 26, 2011 at 6:34 PM, Reginald Tan > wrote: >> I want pstore to be able to do same thing. I've included Enumerable but >> I do have to implement the each method which I dont know how to go >> about. Any suggestions? Thanks > > It looks like, during a transaction, the content of a PStore is > represent by a Hash in the instance variable @table, so you if you > want the PStore to act like a Hash, something like this should work: > > class PStore > def each &blk > in_transaction > @table.each &blk > end > end > > (If you call this without a block, it looks like you get an enumerator > over the state of the store as of the transaction it was captured in, > which can be used outside of that transaction, and that could be > useful for some things.) > > Obviously, this is exploiting details of the implementation rather > than the public interface, so it's at more risk of getting broken in a > new version of the library than would be an approach that relies only > on the public interface. Thanks. I didnt realized before that opening up a class would give you access to instance variables as well. I tried to access the table variable before using self.table but there was no getter method defined for it so I had a bit of trouble. Good catch on - being able to get enum and use it outside transaction. Btw, I find having to do everything in transaction quite annoying and tedious. Do you see any better way of implementing it instead of always passing a block to transaction()? -- Posted via http://www.ruby-forum.com/.