hoodwink.d enhanced

Wed Jun 13

Fully encapsulated class attribute

class Foo @@partially_encapsulated = {} class << self def []=(key, value) @@partially_encapsulated[key] = value end def [](key) @@partially_encapsulated[key] end end end Foo[:name] = 'Marcel' Foo[:name] # => "Marcel" Foo.send(:class_variable_get, :@@partially_encapsulated).clear Foo[:name] # => nil class Bar fully_encapsulated = {} self.class.send(:define_method, :[]=) do |key, value| fully_encapsulated[key] = value end self.class.send(:define_method, :[]) do |key| fully_encapsulated[key] end end Bar[:name] = 'Marcel' Bar[:name] # => "Marcel" # ??? # Challenge: Clear fully_encapsulated # without writing a C extension. # Hint: I have no idea if that's even # possible :) # UPDATE # # Scott Fleckenstein's clever brute # force solution: # http://s3.amazonaws.com/projectionist/scott-fleckenstein.txt # cf. # http://project.ioni.st/post/579#quote_579 # New challenge: Modify the hash without # replacing it :) # UPDATE # Robert Rasmussen taking brute force # to a whole new level: # http://s3.amazonaws.com/projectionist/robert-rasmussen.txt # UPDATE # Jim Weirich writes in with a great solution: # http://s3.amazonaws.com/projectionist/jim-weirich.txt

DIY multitouch for $2

Don't Stop Believing

Petra Haden