Reputation: 31
I was wondering how to properly initialize the subclass "Computer." I want it to inherit the attributes in initialize in the Game class, except for #start, which is a method. I am also unsure of how to handle parameters in the initialize method in this case. Does anyone know an elegant way to rephrase it? Thanks.
class Game
attr_reader :input, :clues
def initialize
colors = %w(R O Y G I V)
code = []
all = ''
count = 0
start
end
def start
...
end
def ask_input
...
end
class Computer < Game
attr_reader :input, :clues
def initialize
colors = %w(R O Y G I V)
code = []
all = ''
count = 0
ask_input
computer_turn
end
.....
end
Upvotes: 1
Views: 680
Reputation: 40
I am also unsure of how to handle parameters in the initialize method in this case
You just
super
in the initializer
of a sub-class to call the initializer
of its super-class. @
char at beginning to make they usable though all instance menthods.attr_reader
from the Computer
class, because it will be inherited from Game
classI want it to inherit the attributes in initialize in the Game class, except for #start, which is a method
#start
of Game
class, I think that you just need to override it in Computer
classResult code
class Game
attr_reader :input, :clues
def initialize
@colors = %w(R O Y G I V)
@code = []
@all = ''
@count = 0
start
end
def ask_input
# sample value for @input
@input = 'sample input'
end
def start
puts "start"
end
end
class Computer < Game
#attr_reader :input, :clues
def initialize
super
ask_input
computer_turn
end
def start
# Do nothing
end
def computer_turn
puts "computer_turn"
p @colors
end
end
comp = Computer.new
# The string "start" is not puts here because Game#start is not called
=> computer_turn
=> ["R", "O", "Y", "G", "I", "V"]
comp.input
=> "sample input"
Upvotes: 1
Reputation: 254
Since you don't want the method start
, just eliminate it from your Game
class so that it wouldn't appear on your subclasses. Something like :
class Game
attr_reader :input, :clues
def initialize
colors = %w(R O Y G I V)
code = []
all = ''
count = 0
(Insert what start does here)
end
def ask_input
...
end
Then, just override the initialize of your Computer
subclass with:
def initialize
colors = %w(R O Y G I V)
code = []
all = ''
count = 0
(insert other functionalities)
end
You can also eliminate the redundant attr_reader
since it has been inherited from Game
Upvotes: 0
Reputation: 45943
I want it to inherit the attributes in initialize in the Game class, except for #start, which is a method.
All attributes and methods will be inherited. You did this correctly with:
class Computer < Game
You don't need the attr_reader
because it is inherited from Game.
I am also unsure of how to handle parameters in the initialize method in this case.
You can do something like the following. It takes an input as the parameter. Consider:
computer = Computer.new( :foo )
After the computer is initialized, it's input
is equal to :foo
.
class Computer < Game
def initialize input
@input = input
...
See:
computer.input
=> :foo
Upvotes: 1