Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
p.162 Extract Surrounding Method;before refactoring
- Loading branch information
Showing
2 changed files
with
64 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,32 @@ | ||
class Person | ||
attr_reader :mother, :children, :name | ||
|
||
def initialize(name, date_of_birth, date_of_death=nil, mother=nil) | ||
@name, @mother = name, mother | ||
@date_of_birth, @date_of_death = date_of_birth, date_of_death | ||
@children = [] | ||
@mother.add_child(self) if @mother | ||
end | ||
|
||
def add_child(child) | ||
@children << child | ||
end | ||
|
||
def number_of_living_descendants | ||
children.inject(0) do |count, child| | ||
count += 1 if child.alive? | ||
count + child.number_of_living_descendants | ||
end | ||
end | ||
|
||
def number_of_descendants_named(name) | ||
children.inject(0) do |count, child| | ||
count += 1 if child.name == name | ||
count+ child.number_of_descendants_named(name) | ||
end | ||
end | ||
|
||
def alive? | ||
@date_of_death.nil? | ||
end | ||
end |
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,32 @@ | ||
require 'rubygems' | ||
require 'test/unit' | ||
require 'must' | ||
require 'kconv' | ||
require 'tempfile' | ||
require 'Extract_Surrounding_Method' | ||
|
||
$KCODE="UTF8" | ||
class TC_Extract_Surrounding_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 | ||
|
||
def setup | ||
@kokeka = Person.new('Kokeka', '2007-09-05',nil, nil) | ||
@kokeko1 = Person.new('Kokeko', '2007-10-05', nil, @kokeka) | ||
@kokekakiikii1 = Person.new('Kokekakiikii', '2007-10-05', nil, @kokeka) | ||
@kokeko2 = Person.new('Kokeko', '2007-11-05', nil, @kokeko1) | ||
@kokekakiikii2 = Person.new('Kokeikakiikii', '2007-11-05', nil, @kokeko1) | ||
@kokekokko = Person.new('Kokekokko', '2007-12-05', nil, @kokeko2) | ||
@kokkokkokko = Person.new('Kokkokkokko','2007-12-05', nil, @kokeko2) | ||
end | ||
|
||
must "test1" do | ||
assert_equal(6, @kokeka.number_of_living_descendants) | ||
assert_equal(2, @kokeka.number_of_descendants_named('Kokeko')) | ||
end | ||
end |