lib/cartesian.rb in Cartesian-0.2.3 vs lib/cartesian.rb in Cartesian-0.3.0
- old
+ new
@@ -136,15 +136,22 @@
# 1,1,1
#
# It also works with Range objects.
#
def **(fixnum)
- return self if fixnum <= 1
- iter = CartesianIterator.new(self, self)
- (fixnum-2).times do
- iter.x(self)
+ if fixnum < 0
+ raise ArgumentError, "power must be non-negative"
+ elsif fixnum == 0
+ return []
+ elsif fixnum == 1
+ return self
+ else
+ iter = CartesianIterator.new(self, self)
+ (fixnum-2).times do
+ iter.x(self)
+ end
+ iter
end
- iter
end
alias :power! :**
end
class Array