On Wed, Apr 27, 2011 at 8:08 AM, Reginald T. [email protected]
wrote:
Christopher D. wrote in post #995252:
On Tue, Apr 26, 2011 at 6:34 PM, Reginald T. [email protected]
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
Regarding your original code
h = {0 => “sword”, 5 => “hammer”, 3 => “arrow”}
puts h.map{ |x| x[0]}.max # gives the highest key which is 5
With a Hash I’d rather
irb(main):002:0> h.keys.max
=> 5
irb(main):003:0> h.max_by {|k,v| k}.first
=> 5
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()?
If you do not want to use transactions you can as well just write a
Hash with Marshal and not use PStore at all. Transaction safety and
persistent consistency is the main feature to use PStore.
A proper implementation of Hash like access that would not depend on
the internals could roughly work like this:
untested!
class PStoreHash
attr_accessor :read_only
def initialize(ps, read_only = false)
@ps = ps
@read_only = read_only
end
def method_missing(*a,&b)
@ps.transaction read_only do |ps|
PSWrap.new(ps).send(*a,&b)
end
end
class PSWrap < SimpleDelegator
include Enumerable
def keys; roots; end
def values; roots.map {|r| self[r]}; end
def each
if block_given?
roots.each {|r| yield r, self[r]}
self
else
to_a
end
end
end
end
Kind regards
robert