Даяна обнови решението на 27.10.2014 16:41 (преди около 10 години)
+class NumberSet
+ include Enumerable
+
+ def initialize(*data)
+ @data = data
+ end
+
+ def each(&block)
+ @data.each(&block)
+ end
+
+ def << (new_element)
+ @data << new_element if not @data.include?(new_element)
+ end
+
+ def size
+ (map { |i| i = 1 }).reduce { |sum, n| sum + n }
+ end
+
+ def empty?
+ size == 0
+ end
+
+ def [](filter)
+ @data.select { |i| i if filter.call(i) }
+ end
+end
+
+
+class Filter
+ def initialize(block = Proc.new)
+ @block = block
+ end
+
+ def call(parameter)
+ @block.call(parameter)
+ end
+
+ def &(other)
+ Filter.new { |item| self.call(item) && other.call(item) }
+ end
+
+ def |(other)
+ Filter.new { |item| self.call(item) || other.call(item) }
+ end
+end
+
+
+class TypeFilter < Filter
+
+ def initialize(type)
+ @type = type
+ end
+
+ def call(parameter)
+ case @type
+ when :integer then parameter.class.equal? Fixnum
+ when :real then parameter.class.equal? Rational or
+ parameter.class.equal? Float
+ when :complex then parameter.class.equal? Complex
+ end
+ end
+end
+
+
+class SignFilter < Filter
+
+ def initialize(sign)
+ @sign = sign
+ end
+
+ def call(parameter)
+ case @sign
+ when :negative then parameter < 0
+ when :positive then parameter > 0
+ when :non_positive then parameter <= 0
+ when :non_negative then parameter >= 0
+ end
+ end
+end