Skip to content

Instantly share code, notes, and snippets.

@u1tnk
Created November 5, 2011 06:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save u1tnk/1341195 to your computer and use it in GitHub Desktop.
Save u1tnk/1341195 to your computer and use it in GitHub Desktop.
tddbc
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
module BaseBall
class Batter
include Comparable
attr_accessor :box, :at_box, :hit, :name
def initialize arg
@box = arg[:box]
@at_box = arg[:at_box]
@hit = arg[:hit]
@name = arg[:name]
end
def average
return 0.000 if @box != 0 and @at_box == 0
return nil if @box == 0
(@hit.to_f / @at_box.to_f).round(3)
end
def average_for_sort
return 0 if @at_box == 0
(@hit.to_f / @at_box.to_f).round(3)
end
def average_format
temp = average
return "---" unless temp
return "1.00" if temp == 1
temp.to_s[1..-1]
end
def <=>(other)
my_average = average_for_sort
other_average = other.average_for_sort
if my_average == 0 and other_average == 0
return (@box <=> other.box) * -1
end
(average_for_sort <=> other.average_for_sort) * -1
end
def to_s
@name
end
end
class Batters
def self.ranking(batters, game_count = nil)
return batters.sort if game_count == nil
[batters.select{|b| (game_count * 3.1).round <= b.box }.sort,
batters.select{|b| (game_count * 3.1).round > b.box }.sort]
end
end
end
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require './baseball.rb'
describe BaseBall do
describe BaseBall::Batter do
before do
@kiyohara = BaseBall::Batter.new({:box => 515, :at_box => 455, :hit => 135, :name => "kiyohara"})
@kawasaki = BaseBall::Batter.new({:box => 655, :at_box => 603, :hit => 161, :name => "kawasaki"})
@perfect_man = BaseBall::Batter.new({:box => 655, :at_box => 655, :hit => 655, :name => "perfect_man"})
@hit_zero_man = BaseBall::Batter.new({:box => 1, :at_box => 1, :hit => 0, :name => "hit_zero_man"})
@zero_man = BaseBall::Batter.new({:box => 0, :at_box => 0, :hit => 0, :name => "zero_man"})
end
it "打席数、打数、安打数を受け取り、打率を計算できること" do
@kiyohara.average.should == 0.297
@kawasaki.average.should == 0.267
end
it "四捨五入、3桁" do
1.12345.round(3).should == 1.123
end
it "打席数が0のときはnil" do
@kiyohara.box = 0
@kiyohara.average().should be_nil
@kiyohara.at_box = 0
@kiyohara.hit = 0
@kiyohara.average().should be_nil
end
it "打席数が0でなく、打数が0のとき0.000" do
@kiyohara.at_box = 0
@kiyohara.average.should == 0.000
end
it "format average" do
@kiyohara.average_format.should == ".297"
end
it "format average 10" do
@perfect_man.average_format.should == "1.00"
end
it "format zero man" do
@zero_man.average_format.should == "---"
end
it "0.333を.333に" do
0.333.to_s[1..-1].should == ".333"
end
it "ranking" do
actual = BaseBall::Batters.ranking [@kiyohara, @kawasaki, @hit_zero_man, @perfect_man, @zero_man]
actual.should == [@perfect_man, @kiyohara, @kawasaki, @hit_zero_man, @zero_man]
end
it "ranking" do
game_count = 10
actual = BaseBall::Batters.ranking([@kiyohara, @kawasaki, @hit_zero_man, @perfect_man, @zero_man], game_count)
actual.should == [[@perfect_man, @kiyohara, @kawasaki], [@hit_zero_man, @zero_man]]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment