From 6fa4b1a451e1636008554941f3beba7fcea5165e Mon Sep 17 00:00:00 2001 From: Hiroshi Kono Date: Sun, 30 May 2010 11:18:52 +0900 Subject: [PATCH] p.159 offeces;before refactoring. --- .../Replace_Loop_with_Collection_Closure_Method.rb | 11 +++++++++-- ..._Replace_Loop_with_Collection_Closure_Method.rb | 14 ++++++++++---- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/chapter6/Replace_Loop_with_Collection_Closure_Method.rb b/chapter6/Replace_Loop_with_Collection_Closure_Method.rb index 00abb25..146de29 100644 --- a/chapter6/Replace_Loop_with_Collection_Closure_Method.rb +++ b/chapter6/Replace_Loop_with_Collection_Closure_Method.rb @@ -1,8 +1,9 @@ class Employees - attr_reader :name - def initialize(name, ismanager) + attr_reader :name, :office + def initialize(name, ismanager, office) @name = name @manager = ismanager + @office = office end def manager? @@ -18,3 +19,9 @@ def test_proc(employees) managers = employees.select{|e| e.manager?} end + +def office_proc(employees) + offices = [] + employees.each{|e| offices << e.office} + return offices +end diff --git a/chapter6/TC_Replace_Loop_with_Collection_Closure_Method.rb b/chapter6/TC_Replace_Loop_with_Collection_Closure_Method.rb index 7ade8f2..1462ed4 100644 --- a/chapter6/TC_Replace_Loop_with_Collection_Closure_Method.rb +++ b/chapter6/TC_Replace_Loop_with_Collection_Closure_Method.rb @@ -17,10 +17,10 @@ def assert(status,msg) must "test_test_proc" do employees = [] - employees << Employees.new("Alan", true) - employees << Employees.new("Barbara", false) - employees << Employees.new("Charlie", false) - employees << Employees.new("Diana",true) + employees << Employees.new("Alan", true, "Japan") + employees << Employees.new("Barbara", false, "Japan") + employees << Employees.new("Charlie", false, "USA") + employees << Employees.new("Diana",true, "USA") manager_name = [] @@ -32,5 +32,11 @@ def assert(status,msg) assert_nil( manager_name.index("Barbara")) assert_nil( manager_name.index("Charlie")) assert_not_nil( manager_name.index("Diana")) + + office_ar= office_proc(employees) + assert_not_nil(office_ar.index("Japan")) + assert_not_nil(office_ar.index("USA")) + end + end