Снежана обнови решението на 22.10.2014 16:31 (преди около 10 години)
+class NumberSet
+ include Enumerable
+
+ attr_reader :container
+
+ def initialize(container = [])
+ @container = container.uniq
+ end
+
+ def <<(number)
+ @container << number
+ end
+
+ def size
+ @container.map { |number| Float(number) rescue number }.uniq.size
+ end
+
+ def empty?
+ @container.empty?
+ end
+
+ def [](criteria)
+ NumberSet.new @container.select { |number| criteria.meets? number }
+ end
+
+ def each
+ @container.each { |number| yield number }
+ end
+end
+
+class Filter
+ def initialize(&condition)
+ @condition = condition
+ end
+
+ def meets?(number)
+ @condition.call number
+ end
+
+ def &(other)
+ Filter.new { |number| meets? number and other.meets? number }
+ end
+
+ def |(other)
+ Filter.new { |number| meets? number or other.meets? number }
+ end
+end
+
+class TypeFilter < Filter
+ attr_reader :condition
+
+ def initialize(condition)
+ @condition = condition
+ end
+
+ def meets?(number)
+ send @condition.to_s + "?", number
+ end
+
+ def integer?(number)
+ number.class == Fixnum or number.class == Bignum
+ end
+
+ def real?(number)
+ number.class == Float or number.class == Rational
+ end
+
+ def complex?(number)
+ number.class == Complex
+ end
+end
+
+class SignFilter < Filter
+ attr_reader :condition
+
+ def initialize(condition)
+ @condition = condition
+ end
+
+ def meets?(number)
+ send @condition.to_s + "?", number
+ end
+
+ def positive?(number)
+ number > 0
+ end
+
+ def non_positive?(number)
+ number <= 0
+ end
+
+ def negative?(number)
+ number < 0
+ end
+
+ def non_negative?(number)
+ number >= 0
+ end
+end