From: Sergey Volkov Date: 2006-05-11T23:31:53+09:00 Subject: Re: plz help with binding ----- Original Message ----- From: Sent: Thursday, May 11, 2006 6:08 AM Subject: Re: plz help with binding >> here is my solution, if you are interested, >> now you can get rid of instance variables in your Ruby code :) > > :) Doesn't this still suffer from the previous problem though? The > "variable" is stored per class, rather than per instance? Do you mean singleton class? If so, then it's the same as per instance. But I need even more - variable stored per add_loc call, I thought I made it, here is how: v = ini is executed every time add_loc is called, so every time new binding for local variable v is created, which is referenced in accessor methods; I can move assignment out of module_eval scope, result is the same: # module M def add_loc name, reader_only=false, ini=nil skl = class << self ; self ; end v = ini # create new local variable binding skl.module_eval{ define_method name.to_sym, lambda{ v } # reader define_method "#{name}=".to_sym, lambda{ |a| v = a } unless reader_only } end end # class CC ; include M ; end ca = CC.new ca.add_loc :pocket, true, [] # array, reader only ca.add_loc :bag ca.pocket << 10 ca.bag = :book # cb = CC.new cb.add_loc :address_book, true, {} # hash, reader only cb.add_loc :bag cb.address_book["me"] = "my address" cb.bag = :apple # p [ca.pocket, ca.bag] p [cb.address_book, cb.bag] ---------- Capture Output ---------- [[10], :book] [{"me"=>"my address"}, :apple] > Terminated with exit code 0. what am I missing here?