Jimmy
Jimmy

Reputation: 51

how to set two instance variables to each other in Ruby

I'm working on Chris Pine's Ruby tutorial. I have to write a cheat method that allows me to set which side of the die I want to show: https://pine.fm/LearnToProgram/?Chapter=09

I have two instance variables: @numberShowing and @numberCheat. @numberCheat receives an input from the user and I want to set @numberShowing to take the value of @numberCheat. However, @numberShowing always outputs some random number. Any tips?

Here's my code so far:

class Die
    def initialize
        roll
    end

    def roll
        @numberShowing = 1 + rand(6)
    end

    def showing
        @numberShowing
    end

    def cheat
        puts "cheat by selecting your die number between 1 and 6"
        @numberCheat = gets.chomp
    end
    @numberShowing = @numberCheat
end
puts Die.new.cheat
puts Die.new.showing

Thanks!

Upvotes: 0

Views: 139

Answers (1)

Chris Cashwell
Chris Cashwell

Reputation: 22859

Move @numberShowing = @numberCheat into the cheat method. But your test is not going to prove it worked. Try something like:

die = Die.new
puts "Currently showing #{die.showing}"
puts "Cheating to change showing number to #{die.cheat}"

Upvotes: 2

Related Questions