Mastermind Code#[]

Hi! I’m confused as to what this spec wants :

#[]
should index into Code#pegs (FAILED - 1)

The method takes in an array, and asks for the string of the pegs. The pegs should be just the string of four PEGS (which they are in the rest of my code, I’ve checked), but for some reason when I pass it to the method it returns an array of four strings with four PEGS. I’ve tried things like uniq, join, and @pegs[0] to get the single string, but it raises an undefined method error for string, even though according to the method I’m manipulating an array. This is very confusing, so I must be understanding the spec wrong. Would appreciate any help to understand the question, since I don’t know what it’s asking for.

~ Mafe

Hi Mafe,

This method is kind of like the [] method in that it will take a position as an argument. However, this position argument will just be a single number because the array you are working with is a 1-D array, your @pegs. Another way to think about this method is an Array.index method for @pegs. For example, say I want the peg at position 3 of your @pegs array, I would pass 3 into your [] method to retrieve that peg. Hope this clarifies a bit about this method.

Michael

@mi-ruan yes, that makes sense, but then how is it that the @pegs are an array? I thought they were supposed to be a 4-char string

@pegs is supposed to be an array. There is a parse method for you to write to convert the 4-char string input to an array and save that array as @pegs.

1 Like

Ohh I see my issue! My parse method was saving each 4-char string into the array, giving me [“BBBB”, “BBBB”, “BBBB”, “BBBB”] which is very wrong. Thanks for the help!

1 Like