Sha256: 7393fe0f4959b0d5946a969baa57f2956b69d1a5c9d59821be862bf60a2f7dfb

Contents?: true

Size: 917 Bytes

Versions: 19

Compression:

Stored size: 917 Bytes

Contents

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

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
facets-1.3.1 lib/facets/core/module/this.rb
facets-1.3.3 lib/facets/core/module/this.rb
facets-1.3.2 lib/facets/core/module/this.rb
facets-1.4.0 lib/facets/core/module/this.rb
facets-1.4.1 lib/facets/core/module/this.rb
facets-1.4.2 lib/facets/core/module/this.rb
facets-1.4.3 lib/facets/core/module/this.rb
facets-1.4.4 lib/facets/core/module/this.rb
facets-1.4.5 lib/facets/core/module/this.rb
facets-1.7.0 lib/facets/core/module/this.rb
facets-1.7.30 lib/facets/core/module/this.rb
facets-1.7.38 lib/facets/core/module/this.rb
facets-1.7.46 lib/facets/core/module/this.rb
facets-1.8.0 lib/facets/core/module/this.rb
facets-1.8.20 lib/facets/core/module/this.rb
facets-1.8.49 lib/facets/core/module/this.rb
facets-1.8.51 lib/facets/core/module/this.rb
facets-1.8.54 lib/facets/core/module/this.rb
facets-1.8.8 lib/facets/core/module/this.rb