Мая обнови решението на 26.10.2014 21:46 (преди около 10 години)
+class NumberSet
+ include Enumerable
+
+ def initialize(new_set = [])
+ @internal_set = new_set.uniq
+ end
+
+ def <<(element)
+ @internal_set << element unless @internal_set.include? element
+ end
+
+ def each
+ @internal_set.each { |element| yield element }
+ end
+
+ def size
+ @internal_set.size
+ end
+
+ def empty?
+ @internal_set.size == 0
+ end
+
+ def [](filter)
+ NumberSet.new @internal_set.select { |element| filter[element] }
+ end
+end
+
+class Filter
+ def initialize(&block)
+ @block = block
+ end
+
+ def [](element)
+ @block.call element
+ end
+
+ def &(other)
+ Filter.new { |element| self[element] && other[element] }
+ end
+
+ def |(other)
+ Filter.new { |element| self[element] || other[element] }
+ end
+end
+
+class TypeFilter < Filter
+ def initialize(option)
+ @block = case option
+ when :integer then ->element { element.class.superclass == Integer }
+ when :real then ->element { [Float, Rational].include? element.class }
+ when :complex then ->element { element.class == Complex }
+ end
+ end
+end
+
+class SignFilter < Filter
+ def initialize(option)
+ @block = case option
+ when :positive then ->element { element > 0 }
+ when :non_positive then ->element { element <= 0 }
+ when :negative then ->element { element < 0 }
+ when :non_negative then ->element { element >= 0 }
+ end
+ end
+end