diff --git a/spec/std/range_spec.cr b/spec/std/range_spec.cr index c3307ddf3997..9b89f2cd4765 100644 --- a/spec/std/range_spec.cr +++ b/spec/std/range_spec.cr @@ -203,7 +203,7 @@ describe "Range" do end end - describe "each" do + describe "#each" do it "gives correct values with inclusive range" do range = -1..3 arr = [] of Int32 @@ -236,10 +236,14 @@ describe "Range" do end it "raises on beginless" do - range = (true ? nil : 1)..4 expect_raises(ArgumentError, "Can't each beginless range") do - range.each { } + (..4).each { } end + typeof((..4).each { |x| break x }).should eq Nil + expect_raises(ArgumentError, "Can't each beginless range") do + (nil.as(Int32?)..4).each { } + end + typeof((nil.as(Int32?)..4).each { |x| break x }).should eq Int32? end it "doesn't have Nil as a type for endless each" do @@ -251,7 +255,7 @@ describe "Range" do end end - describe "reverse_each" do + describe "#reverse_each" do it "gives correct values with inclusive range" do range = 'a'..'c' arr = [] of Char @@ -274,9 +278,11 @@ describe "Range" do end it "raises on endless range" do - range = (3..(true ? nil : 1)) expect_raises(ArgumentError, "Can't reverse_each endless range") do - range.reverse_each { } + (3..).reverse_each { } + end + expect_raises(ArgumentError, "Can't reverse_each endless range") do + (3..nil.as(Int32?)).reverse_each { } end end @@ -291,7 +297,7 @@ describe "Range" do end end - describe "each iterator" do + describe "#each iterator" do it "does next with inclusive range" do a = 1..3 iter = a.each @@ -317,9 +323,11 @@ describe "Range" do end it "raises on beginless range" do - r = (true ? nil : 1)..3 expect_raises(ArgumentError, "Can't each beginless range") do - r.each + (..3).each + end + expect_raises(ArgumentError, "Can't each beginless range") do + (nil.as(Int32?)..3).each end end @@ -344,7 +352,7 @@ describe "Range" do end end - describe "reverse_each iterator" do + describe "#reverse_each iterator" do it "does next with inclusive range" do a = 1..3 iter = a.reverse_each @@ -393,21 +401,33 @@ describe "Range" do it "raises on endless range" do expect_raises(ArgumentError, "Can't reverse_each endless range") do - (1..(true ? nil : 1)).reverse_each + (1..).reverse_each + end + expect_raises(ArgumentError, "Can't reverse_each endless range") do + (1..nil.as(Int32?)).reverse_each end end end - describe "sample" do + describe "#sample" do it "raises on open range" do expect_raises(ArgumentError, "Can't sample an open range") do - (1..(true ? nil : 1)).sample + (1..).sample + end + expect_raises(ArgumentError, "Can't sample an open range") do + (1..nil.as(Int32?)).sample + end + expect_raises(ArgumentError, "Can't sample an open range") do + (..1).sample + end + expect_raises(ArgumentError, "Can't sample an open range") do + (nil.as(Int32?)..1).sample end expect_raises(ArgumentError, "Can't sample an open range") do - ((true ? nil : 1)..1).sample + (..).sample end expect_raises(ArgumentError, "Can't sample an open range") do - ((true ? nil : 1)..(true ? nil : 1)).sample + (nil.as(Int32?)..nil.as(Int32?)).sample end end @@ -648,7 +668,7 @@ describe "Range" do end end - describe "size" do + describe "#size" do it "optimizes for int range" do (5..12).size.should eq(8) (5...12).size.should eq(7) @@ -661,13 +681,19 @@ describe "Range" do it "raises on beginless range" do expect_raises(ArgumentError, "Can't calculate size of an open range") do - ((true ? nil : 1)..3).size + (..3).size + end + expect_raises(ArgumentError, "Can't calculate size of an open range") do + (nil.as(Int32?)..3).size end end it "raises on endless range" do expect_raises(ArgumentError, "Can't calculate size of an open range") do - (3..(true ? nil : 1)).size + (3..).size + end + expect_raises(ArgumentError, "Can't calculate size of an open range") do + (3..nil.as(Int32?)).size end end end diff --git a/src/range.cr b/src/range.cr index d09b91380b3f..db1b3f32902a 100644 --- a/src/range.cr +++ b/src/range.cr @@ -109,10 +109,6 @@ struct Range(B, E) # # prints: 10 11 12 13 14 15 # ``` def each(&) : Nil - {% if B == Nil %} - {% raise "Can't each beginless range" %} - {% end %} - current = @begin if current.nil? raise ArgumentError.new("Can't each beginless range") @@ -142,10 +138,6 @@ struct Range(B, E) # (1..3).each.skip(1).to_a # => [2, 3] # ``` def each - {% if B == Nil %} - {% raise "Can't each beginless range" %} - {% end %} - if @begin.nil? raise ArgumentError.new("Can't each beginless range") end @@ -161,10 +153,6 @@ struct Range(B, E) # # prints: 14 13 12 11 10 # ``` def reverse_each(&) : Nil - {% if E == Nil %} - {% raise "Can't reverse_each endless range" %} - {% end %} - end_value = @end if end_value.nil? raise ArgumentError.new("Can't reverse_each endless range") @@ -196,10 +184,6 @@ struct Range(B, E) # (1..3).reverse_each.skip(1).to_a # => [2, 1] # ``` def reverse_each - {% if E == Nil %} - {% raise "Can't reverse_each endless range" %} - {% end %} - if @end.nil? raise ArgumentError.new("Can't reverse_each endless range") end @@ -363,10 +347,6 @@ struct Range(B, E) # # Raises `ArgumentError` if `self` is an open range. def sample(random : Random = Random::DEFAULT) - {% if B == Nil || E == Nil %} - {% raise "Can't sample an open range" %} - {% end %} - {% if B < Int && E < Int %} random.rand(self) {% elsif B < Float && E < Float %} @@ -391,10 +371,6 @@ struct Range(B, E) # once. Thus, *random* will be left in a different state compared to the # implementation in `Enumerable`. def sample(n : Int, random = Random::DEFAULT) - {% if B == Nil || E == Nil %} - {% raise "Can't sample an open range" %} - {% end %} - if self.begin.nil? || self.end.nil? raise ArgumentError.new("Can't sample an open range") end @@ -507,10 +483,6 @@ struct Range(B, E) # (3...8).size # => 5 # ``` def size - {% if B == Nil || E == Nil %} - {% raise "Can't calculate size of an open range" %} - {% end %} - b = self.begin e = self.end