November 12, 2006

with

#
# with() - Equivalent of Pascal or VB 'WITH'
#
# Instead of writing ...
#       a.width = 1
#       a.height= 2
#       a.name  = "hello"
# ... write
#       with(a){
#           width = 1
#           height= 2
#           name  = "hello"
#       }
#
# Another example:
#
#    class A
#        def method_missing(name, *args)
#            puts "caught: #{name}(#{args.map{|x| x.to_s}.join(',')})"
#        end
#        
#        def func(x)
#            puts "A:func(#{x})"
#        end
#    end
#    
#    a = A.new
#    
#    with(a){
#        func(3)
#        g(4,5,6,"string")
#    }
#    
#    => 
#        A:func(3)
#        caught: g(4,5,6,string)
#
#
#

def with(a, &block)
    a.instance_eval &block
end

Posted by laza at 04:07 AM | Comments (0) | TrackBack