Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
p.158 "Replace Loop with Collection Closure Method";before refactoring.
- Loading branch information
Showing
2 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
36
chapter6/TC_Replace_Loop_with_Collection_Closure_Method.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |