Константин обнови решението на 24.10.2014 01:15 (преди около 10 години)
+class NumberSet
+ include Enumerable
+
+ def initialize
+ @numbers = []
+ end
+
+ def <<(number)
+ unless @numbers.include?(number)
+ @numbers << number
+ end
+ end
+
+ def size
+ @numbers.length
+ end
+
+ def empty?
+ self.size == 0
+ end
+
+ def [](other)
+ @numbers.select do |number|
+ number if other.filter.call(number)
+ end
+ end
+
+ def each
+ @numbers.each do |number|
+ number if yield number
+ end
+ end
+end
+
+class Filter
+ attr_accessor :filter
+
+ def initialize(&given_filter)
+ @filter = given_filter
+ end
+
+ def &(other)
+ Filter.new { |x| filter.call(x) && other.filter.call(x) }
+ end
+
+ def |(other)
+ Filter.new { |x| filter.call(x) || other.filter.call(x) }
+ end
+end
+
+class TypeFilter < Filter
+ def initialize type
+ case type
+ when :integer then @filter = get_integer_filter()
+ when :real then @filter = get_real_filter()
+ when :complex then @filter = get_complex_filter()
+ end
+ end
+
+ def get_integer_filter
+ Proc.new { |n| n if n.is_a? Integer }
+ end
+
+ def get_complex_filter
+ Proc.new { |n| n if n.is_a? Complex }
+ end
+
+ def get_real_filter
+ Proc.new { |n| n if n.is_a? Float or n.is_a? Rational}
+ end
+
+ def get_lambda type
+ check = lambda { |n| n if n.is_a? Integer}
+ end
+end
+
+class SignFilter < Filter
+ def initialize selection
+ case selection
+ when :positive then @filter = get_positive_filter(false)
+ when :non_negative then @filter = get_positive_filter(true)
+ when :negative then @filter = get_negative_filter(false)
+ when :non_positive then @filter = get_negative_filter(true)
+ end
+ end
+
+ def get_positive_filter include_zero
+ Proc.new { |n| n if n > 0 || (include_zero && n == 0) }
+ end
+
+ def get_negative_filter include_zero
+ Proc.new { |n| n if n < 0 || (include_zero && n == 0) }
+ end
+end