Sha256: 8b684055ecb70e9e94ab5f5bcbba2760e52ec8ef537fcff9c83de0a3a3e2ec88

Contents?: true

Size: 979 Bytes

Versions: 1

Compression:

Stored size: 979 Bytes

Contents

class Future
	VERSION = "0.0.0"

	def initialize(obj, name, args, block)
		@obj, @name, @args, @block = obj, name, args, block
		@th  = Thread.start do
			Thread.pass # 一応明示的に pass しておく
			@obj.send(@name, *@args, &@block)
		end
	end

	def method_missing(name, *args, &block)
		@th.value.send(name, *args, &block)
	end

	# 全部委譲してなりすます
	Object.instance_methods.each do |m|
		next if %w|__send__ __id__ object_id|.include? m.to_s
		undef_method m
	end

	module ObjectExtension
		def async(name=nil, *args, &block)
			name ? Future.new(self, name, args, block) : AsyncObject.new(self)
		end

		class AsyncObject
			def initialize(obj)
				@obj = obj
			end

			def method_missing(name, *args, &block)
				Future.new(@obj, name, args, block)
			end

			Object.instance_methods.each do |m|
				next if %w|__send__ __id__ object_id|.include? m.to_s
				undef_method m
			end
		end
	end
end

class Object
	include Future::ObjectExtension
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cho45-future-0.0.0 lib/future.rb