[ruby-talk:02358] Re: setter() for local variables

From: ts <decoux@...>
Date: 2000-04-04 11:17:22 UTC
List: ruby-talk #2358
>>>>> "Y" == Yukihiro Matsumoto <matz@netlab.co.jp> writes:

Y>   module DBD
Y>     class Marshal
Y>       def initialize(db)
Y>         @db = db
Y>         @tbl = {}
Y>       end
Y>       def []=(k,v)
Y>         @tbl[k] = v
Y>       end
Y>       def []
Y>         @tbl[k]
Y>       end
Y>       def close
Y>         for k,v in @tbl
Y>           @db[::Mashal::dump(k)] = ::Mashal::dump(v)
Y>         end
Y>         @db.close
Y>     end
Y>   end

 I just need to write this, no ?

    class Toto
        def initialize(db, key, value)
            @db, @key, @value = db, key, value
            @value
        end
        def method_missing(id, *args)
            @value = @db[@key]
            result = @value.send(id, *args)
            @db[@key] = @value
            result
        end
    end

 and with :
 
  db = BDB::Btree.open("aa")
  db[1] = [1, 2]
  db[1].push(3)

 db[1] will return a new object Toto.

 Only the current object is duplicated (i.e. in memory and in the db file)
 and not the whole db.

 In this case, do I need to destroy all methods in the class Toto, to have
 only method_missing ?

 Do this work for all objects or I'm missing something ?


Guy Decoux

In This Thread