# frozen_string_literal: truemoduleRBSmoduleUnitTestmoduleWithAliasesincludeConvertiblesclassWithEnumincludeEnumerabledefinitialize(enum)=@enum=enumdefeach(&block)=@enum.each(&block)defand_nil(&block)self.and(nil,&_=block)enddefbut(*cases,&block)returnWithEnum.newto_enum(__method__||raise,*cases)unlessblockeachdo|arg|yieldargunlesscases.any?{(_=_1)===arg}endenddefand(*args,&block)returnWithEnum.newto_enum(__method__||raise,*args)unlessblockeach(&block)args.eachdo|arg|ifWithEnum===arg# use `===` as `arg` might not have `.is_a?` on itarg.each(&block)elseblock.call(_=arg)endendendenddefwith(*args,&block)returnWithEnum.newto_enum(__method__||raise,*args)unlessblockargs.each(&block)enddefwith_int(value=3,&block)returnWithEnum.newto_enum(__method__||raise,value)unlessblockyieldvalueyieldToInt.new(value)enddefwith_float(value=0.1)returnWithEnum.newto_enum(__method__||raise,value)unlessblock_given?yieldvalueyieldToF.new(value)enddefwith_string(value='')returnWithEnum.newto_enum(__method__||raise,value)unlessblock_given?yieldvalueyieldToStr.new(value)enddefwith_array(*elements)returnWithEnum.newto_enum(__method__||raise,*elements)unlessblock_given?yield_=elementsyieldToArray.new(*elements)enddefwith_hash(hash={})returnWithEnum.newto_enum(__method__||raise,hash)unlessblock_given?yield_=hashyieldToHash.new(hash)enddefwith_io(io=$stdout)returnWithEnum.newto_enum(__method__||raise,io)unlessblock_given?yieldioyieldToIO.new(io)enddefwith_path(path="/tmp/foo.txt",&block)returnWithEnum.newto_enum(__method__||raise,path)unlessblockwith_string(path,&block)block.callToPath.new(path)enddefwith_encoding(encoding=Encoding::UTF_8,&block)returnWithEnum.newto_enum(__method__||raise,encoding)unlessblockblock.callencodingwith_string(encoding.to_s,&block)enddefwith_interned(value=:&,&block)returnWithEnum.newto_enum(__method__||raise,value)unlessblockwith_string(value.to_s,&block)block.callvalue.to_symenddefwith_bool(&block)returnWithEnum.newto_enum(__method__||raise)unlessblockyieldtrueyieldfalseenddefwith_boolish(&block)returnWithEnum.newto_enum(__method__||raise)unlessblockwith_bool(&block)[nil,1,Object.new,BlankSlate.new,"hello, world!"].each(&block)endaliaswith_untypedwith_boolishdefwith_range(start,stop,exclude_end=false)# If you need fixed starting and stopping points, you can just do `with_range with(1), with(2)`.raiseArgumentError,'`start` must be from a `with` method'unlessstart.is_a?WithEnumraiseArgumentError,'`stop` must be from a `with` method'unlessstop.is_a?WithEnumstart.eachdo|lower|stop.eachdo|upper|yieldCustomRange.new(lower,upper,exclude_end)# `Range` requires `begin <=> end` to return non-nil, but doesn't actually# end up using the return value of it. This is to add that in when needed.unlessdefined?lower.<=>deflower.<=>(rhs)=:not_nil# steep:ignore MethodDefinitionInUndeclaredModuleend# If `lower <=> rhs` is defined but nil, then that means we're going to be constructing# an illegal range (eg `3..ToInt.new(4)`). So, we need to skip yielding an invalid range# in that case.nextifdefined?(lower.<=>)&&nil==(lower<=>upper)yieldRange.new(lower,upper,exclude_end)endendendendendend