Class: Cricket::Player

Inherits:
Object
  • Object
show all
Defined in:
lib/cricket/player.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Player

Returns a new instance of Player.



5
6
7
8
9
10
# File 'lib/cricket/player.rb', line 5

def initialize(name)
	@name = name.capitalize
	types_of_batsman = ["Test", "Odi", "T20"]
	@specialist = types_of_batsman[rand(0..2)]
	@sponsors = Hash.new(0)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/cricket/player.rb', line 3

def name
  @name
end

#specialistObject (readonly)

Returns the value of attribute specialist.



3
4
5
# File 'lib/cricket/player.rb', line 3

def specialist
  @specialist
end

Instance Method Details

#all_sponsors(sponsor) ⇒ Object



44
45
46
# File 'lib/cricket/player.rb', line 44

def all_sponsors(sponsor)
	@sponsors[sponsor.name] += sponsor.earn
end

#earningObject



48
49
50
# File 'lib/cricket/player.rb', line 48

def earning
	@sponsors.values.reduce(0, :+)
end

#nicknameObject



16
17
18
# File 'lib/cricket/player.rb', line 16

def nickname
	@name + "India"
end

#playODI(balls) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/cricket/player.rb', line 28

def playODI(balls)
	if @specialist == "Odi"
		run = (balls * 1.1).to_i
	else
		run = (balls * 1)
	end
end

#playT20(balls) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/cricket/player.rb', line 36

def playT20(balls)
	if @specialist == "T20"
		run = (balls * 1.8).to_i
	else
		run = (balls * 1.2).to_i
	end
end

#playTest(balls) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/cricket/player.rb', line 20

def playTest(balls)
	if @specialist == "Test"
		run = (balls * 0.7).to_i
	else
		run = (balls * 0.5).to_i
	end
end

#to_sObject



12
13
14
# File 'lib/cricket/player.rb', line 12

def to_s
	"This is #{@name}, a #{@specialist.to_s} specialist, nicknamed #{nickname}."
end