1

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
catcher
  • 31
  • 1
  • 3
  • One way - don't even do it in `initialize`, but define a custom setter, e.g. `def colors; @colors ||= %w(R O Y G I V); end` on `Game`, which will then be available in `Computer`. – Kimball May 29 '15 at 01:42
  • Though I'd generally recommend restructuring your variables into custom setter methods for cleaner inheritance, see [this question](http://stackoverflow.com/questions/10728735/inherit-class-level-instance-variables-in-ruby) for answers to the question as asked. – Kimball May 29 '15 at 01:49

3 Answers3

1

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
B Seven
  • 44,484
  • 66
  • 240
  • 385
1

I am also unsure of how to handle parameters in the initialize method in this case

You just

  • Add super in the initializer of a sub-class to call the initializer of its super-class.
  • And for sure, all instance variables should have @ char at beginning to make they usable though all instance menthods.
  • Also remove attr_reader from the Computer class, because it will be inherited from Game class

I want it to inherit the attributes in initialize in the Game class, except for #start, which is a method

  • Finally, to avoid call the method #start of Game class, I think that you just need to override it in Computer class

Result 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"
pddo
  • 40
  • 6
0

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