Любомир обнови решението на 26.10.2014 12:20 (преди около 11 години)
+class NumberSet
+  include Enumerable
+
+  def initialize
+    @results_array = []
+  end
+
+  def <<(val)
+    already_have_this = false
+    @results_array.each do |element|
+      already_have_this = true if element == val
+    end
+    @results_array << val unless already_have_this
+  end
+
+  def each(&block)
+    @results_array.each(&block)
+  end
+
+  def size
+    counter = 0
+    @results_array.each { |element| counter += 1 }
+    counter
+  end
+
+  def empty?
+    @results_array.empty?
+  end
+
+  def [](var)
+    @results_array.select(&var.lambda_function)
+  end
+end
+
+class AbstractFilter
+  attr_accessor :lambda_function
+
+  def &(next_filter)
+    res = AbstractFilter.new
+    first = self.lambda_function
+    second = next_filter.lambda_function
+    res.lambda_function = lambda { |x| first.call x and second.call x}
+    res
+  end
+
+  def |(next_filter)
+    res = AbstractFilter.new
+    first = self.lambda_function
+    second = next_filter.lambda_function
+    res.lambda_function = lambda { |x| first.call x or second.call x}
+    res
+  end
+
+end
+
+class TypeFilter < AbstractFilter
+  def initialize(type)
+    real_type = lambda { |x| x.class == Rational or x.class == Float }
+    case type
+      when :integer then @lambda_function = lambda { |x| x.class == Fixnum }
+      when :complex then @lambda_function = lambda { |x| x.class == Complex }
+      when :real then @lambda_function = real_type
+     end
+  end
+end
+
+
+class SignFilter < AbstractFilter
+  def initialize(sign)
+    case sign
+      when :non_negative then @lambda_function = lambda { |x| x >= 0 }
+      when :non_positive then @lambda_function = lambda { |x| x <= 0 }
+      when :negative  then @lambda_function = lambda { |x| x < 0 }
+      when :positive then @lambda_function = lambda { |x| x > 0 }
+    end
+  end
+end
+
+class Filter < AbstractFilter
+  def initialize(&expression)
+    @lambda_function = expression
+  end
+end
Здрасти,
Това са нещата, които виждам като проблеми в кода ти.
- Дай малко по-добри имена от 
results_arrayиval. В контекста на числа и множества работим, ще намериш нещо по-подходящо. - За начина, по който изчисляваш 
already_have_thisвече има метод вArray. :) - За начина, по който изчисляваш 
sizeвече има метод вArray. - Връщаш 
Arrayобект отNumberSet. Ние очаквамеNumberSetобратно. - Защо ти е този 
AbstractFilter? Не може ли да се слее сFilter? - Залагам, че можеш да реализираш 
Filter#&иFilter#|с анонимна функция и ще стане по-елегантно. Помисли си как можеш да ползвашFilterкласа за целта. 
