Resource allocation/deallocation (cont'd) 1 # Method 2: Use blocks (like C++''s RAII) 2 class Foo 3 def initialize() 4 @x = allocate_some_resource 5 end 6 7 def finalize() 8 deallocate_some_resource(@x) 9 end 10 11 def self.with 12 foo = Foo.new 13 begin 14 yield 15 ensure 16 foo.finalize 17 end 18 end 19 end