Mastermind question

Just curious - I think it would be cool for the player to be able to see what the secret code had been if they fail to guess correctly. I want to include the code in a string before exiting the game, but it usually looks like this:

Whoops - better luck next time! The code was #<Code:0x007ff8b9084c60>

If I p the secret code I get:

#<Code:0x007ff8b9084c60 @pegs=["Y", "G", "B", "G"]>

Anyone have any idea how to access that “YGBG” and puts it? Thanks! :grin:

Laurette, you can use string interpolation to get the value of the @pegs instance variable, provided that you have an attr_reader for pegs in the Code class.

Something like "The code was #{code.pegs}" is what you’re looking for. I encourage you to Google “ruby string interpolation” for more information.

@oalvarez Right! I’ve been using interpolation of course and have the attr_reader on :pegs in the Code class, but was just struggling to print out the right answer.

Looks like the way to do it was #{secret_code[-4…-1].join} :joy: