759: def EventMachine::attach io, handler=nil, *args
760: klass = if (handler and handler.is_a?(Class))
761: raise ArgumentError, 'must provide module or subclass of EventMachine::Connection' unless Connection > handler
762: handler
763: else
764: Class.new( Connection ) {handler and include handler}
765: end
766:
767: arity = klass.instance_method(:initialize).arity
768: expected = arity >= 0 ? arity : -(arity + 1)
769: if (arity >= 0 and args.size != expected) or (arity < 0 and args.size < expected)
770: raise ArgumentError, "wrong number of arguments for #{klass}#initialize (#{args.size} for #{expected})"
771: end
772:
773: readmode = klass.public_instance_methods.any?{|m| m.to_sym == :notify_readable }
774: writemode = klass.public_instance_methods.any?{|m| m.to_sym == :notify_writable }
775:
776: s = attach_fd io.respond_to?(:fileno) ? io.fileno : io, readmode, writemode
777:
778: c = klass.new s, *args
779: @conns[s] = c
780: block_given? and yield c
781: c
782: end