Skip to content

Commit

Permalink
p.158 "Replace Loop with Collection Closure Method";before refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
kono committed May 30, 2010
1 parent a4897d8 commit 483d0b2
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
24 changes: 24 additions & 0 deletions chapter6/Replace_Loop_with_Collection_Closure_Method.rb
@@ -0,0 +1,24 @@
class Employees
attr_reader :name
def initialize(name, ismanager)
@name = name
@manager = ismanager
end

def manager?
if @manager
true
else
false
end
end
end

def test_proc(employees)

managers = []
employees.each do |e|
managers << e if e.manager?
end
managers
end
36 changes: 36 additions & 0 deletions chapter6/TC_Replace_Loop_with_Collection_Closure_Method.rb
@@ -0,0 +1,36 @@
require 'rubygems'
require 'test/unit'
require 'must'
require 'kconv'
require 'Replace_Loop_with_Collection_Closure_Method'

$KCODE="UTF8"
class Replace_Loop_with_Collection_Closure_Method < Test::Unit::TestCase

def assert(status,msg)
if(RUBY_PLATFORM.downcase =~ /mswin(?!ce)|mingw|cygwin|bccwin/)
msg=Kconv.tosjis(msg)
end
super(status,msg)
end


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)

manager_name = []

test_proc(employees).each do |e|
manager_name << e.name
end

assert_not_nil( manager_name.index("Alan"))
assert_nil( manager_name.index("Barbara"))
assert_nil( manager_name.index("Charlie"))
assert_not_nil( manager_name.index("Diana"))
end
end

0 comments on commit 483d0b2

Please sign in to comment.