
require 'facets/more/functor'

#
#   class X
#
#    this.y = "Hello"
#
#    this.up = lambda { puts @y.upcase }
#
#   end
#
#   X.new.up  #=> HELLO
#

class Module

  def this
    klass = self
    Functor.new do |op, *args|
      case op.to_s[-1,1]
      when '='
        op = op.to_s.chomp('=')
        if Proc === args[0]
          define_method( op, &args[0] )
        else
          define_method( op ) do
            r = instance_variable_set( "@#{op}", args[0] )
            klass.class.class_eval %{
              def #{op}; @#{op}; end
            }
            r
          end
        end
      else
        klass.class_eval %{
          def #{op}; @#{op}; end
        }
      end
    end
  end

end


=begin test

  require 'test/unit'

  class TCthis < Test::Unit::TestCase

    class Foo
      this.bar = 10
    end

    def test01
      x = Foo.new
      assert_equal( 10, x.bar )
    end

  end

=end

