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