Super exciting, I know. I saw this problem this morning and my eyes glazed over, for whatever reason. After trying my hardest to figure it out for a very very long time, I ended up caving and had to take a peek at the solution. To be honest, I probably wouldn’t have figured it out on my own. It’s that whole ‘two-layer problem’ thing going on, I know it! I just don’t feel that I’m totally grasping ruby just yet, but that’s okay. After much time spent looking at the problem and really digesting it, and also talking with another student about it, I think I’ve got it. Thank you to Melissa for your patience and letting me explain it to you in the best way I could. Here is my best take of understanding:

We have a hash called ‘produce’. We have 4 key-value pairs, each key is a string that points to its value that is either a ‘Fruit’ or ‘Vegetable’. Next the method ‘select_fruit’ is being created that takes one parameter, in this case it is called ‘produce_list’. After that, three things occurs. First, we assign the return value of calling #keys on produce_list, which is an array, to the variable ‘produce_keys’. So now we have an array of produce keys: [‘apple’, ‘carrot’, ‘pear’, ‘broccoli’]. Then we create an empty hash and call it ‘selected_fruits’.
We enter the loop now. The first thing to do is to add an if condition statement that breaks out of the loop if that conditions is met. It says to break out of the loop if the counter (which is counter = 0) is equal to the size of the ‘produce_keys’ array. This will save a lot of work if the array turns out to be empty. Next we create a variable called ‘current_key’ that is assigned to the key at the current produce_keys[counter]. Meaning, since arrays count by zero-index, it will start at 0 and run for the length of the array. The element at the 0th index of ‘produce_keys’ is the first element to be assigned to the ‘current_key’ variable. ‘current_value’ is being assigned in I believe the same way, but this time to the produce_list hash that will be passed in, and to its value. Whatever key is being assigned to ‘current_key’, it’s corresponding value will be assigned to ‘current_value’. Now we have the key and it’s value, in the first instance, we will have ‘apple’ => ‘Fruit’.
Now we get to the ‘if’ statement. This says that if the value of the value is equal to ‘Fruit’ (in the first instance with apple, it is), then we simply replicate this key-value pair into the new ‘selected_fruit’ hash. Increment the counter after this to continue on to the next elements in the array until it’s done. If the value doesn’t match ‘Fruit’, nothing will get added, and it moves on to the next element. When we pass in the ‘produce’ hash, all of this work will take place and what is returned is a new hash:
selected_fruits = { ‘apple’ => ‘Fruit’, ‘pear’ => ‘Fruit’ }
Super cool.