Методи обнови решението на 24.10.2014 11:59 (преди около 10 години)
+class NumberSet
+ include Enumerable
+
+ def initialize
+ @unique_numbers = []
+ end
+
+ def each
+ @unique_numbers.each {|number| yield number}
+ end
+
+ def <<(new_number)
+ @unique_numbers << new_number if @unique_numbers.all? do |number|
+ number != new_number
+ end
+ end
+
+ def size
+ @unique_numbers.size
+ end
+
+ def empty?
+ @unique_numbers.empty?
+ end
+
+ def [](filter)
+ select { |element| element if filter.criteria.(element)}
+ end
+
+end
+
+class Filter
+
+ attr_reader :criteria
+
+ def initialize(&block)
+ @criteria = block
+ end
+
+ def &(new_filter)
+ Filter.new do |element|
+ criteria.(element) and new_filter.criteria.(element)
+ end
+ end
+
+ def |(new_filter)
+ Filter.new do |element|
+ criteria.(element) or new_filter.criteria.(element)
+ end
+ end
+end
+
+class TypeFilter < Filter
+ def initialize(member_type)
+ case member_type
+ when :integer then @criteria = ->(member) { member.is_a? Integer }
+ when :real then @criteria = ->(member) { member.is_a? Float }
+ when :complex then @criteria = ->(member) { member.is_a? Rational }
+ end
+ end
+end
+
+class SignFilter < Filter
+ def initialize(sign)
+ case sign
+ when :positive then @criteria = ->(number) { number > 0 }
+ when :non_positive then @criteria = ->(number) { number <= 0 }
+ when :negative then @criteria = ->(number) { number < 0 }
+ when :non_negative then @criteria = ->(number) { number >= 0 }
+ end
+ end
+end