????Ruby??????е?Enumerable????????????????????????????????t??????????????????????Enumerable??????÷?????

????The Enumerable mixin provides collection classes with several traversal and searching methods?? and with the ability to sort. The class must provide a method each?? which yields successive members of the collection. If Enumerable#max?? min?? or sort is used?? the objects in the collection must also implement a meaningful <=> operator?? as these methods rely on an ordering between members of the collection.

????Enumerable??????????е????????????

  ???Enumerable??????????????2??裺

????·mixin Enumerable??飻

????·?????Enumerable???????ж???each?????????each????????????????е?????????

????·?????????max min sort????????????????????<=>??????

????<=>?????????1???????????other?????self > other??????1??self == other????0????????-1???????????????????????????????????????????????????????????...??

??????????????÷????????????????????????——????????????????????????????????Enumerable????ж???????????????????????????????

require 'pp'
Object.method_undefined(:assert_true) if Object.method_defined?(:assert_true)
Object.method_undefined(:assert_false) if Object.method_defined?(:assert_false)
Object.module_eval do
def assert_true
self == true
end
def assert_false
self == false
end
end
class TestCase
attr_accessor :priority
attr_reader :execute_time?? :name?? :test_result
def initialize(name?? priority?? &case_step)
@name = name
@priority = priority
@case_step = case_step
end
def <=>(other)
@priority <=> other.priority
end
def execute
@test_result = @case_step.call
@execute_time = @priority
end
end
class TestCaseGroup
include Enumerable
attr_reader :test_cases?? :test_result
def initialize
@test_cases = []
@test_result = {}
end
def add_case *test_case
test_case.each do |c|
@test_cases << c
end
end
def delete_case test_cases
@test_cases.delete test_cases
end
def each(&block)
@test_cases.each &block
end
def execute
@test_cases.each do |c|
@test_result[c.name.to_sym] = c.execute
end
end
end
#???????????
case1 = TestCase.new('case1'?? 1) do
(1 > 1).assert_false
end
case2 = TestCase.new('case2'?? 2) do
(1 > 1).assert_true
end
case3 = TestCase.new('case3'?? 3) do
(1 == 1).assert_true
end
#?????????鰱???????
test_group = TestCaseGroup.new
test_group.add_case case1?? case2?? case3
#???????????????
test_group.execute
p test_group.test_result
#?????case???????????2??
#from Enumerable
pp test_group.any? {|c| c.execute_time > 2}
#?????case???
#from Enumerable
pp test_group.any? {|c| c.test_result.eql? false}
#????????????
#from Enumerable
puts test_group.inject(0){|total?? c|total + c.execute_time}
#?????????????
#С?????2????飬?????????
pp test_group.partition{|c| c.execute_time < 2}

????Enumerable???????????????????????????????????any? all? inject partition????????????????

????????????each_with_index??collect??????????????????????Enumerable?????????1????????1????顣