What is X and Y in this exercise?


#1


Hi, I don't understand what is X and Y here in this exercise! Could anyone please explain what they are and what they do?


https://www.codecademy.com/en/courses/ruby-beginner-en-F3loB/2/1?curriculum_id=5059f8619189a5000201fbcb

Here is the code:

friends = ["Milhouse", "Ralph", "Nelson", "Otto"]

family = { "Homer" => "dad",
  "Marge" => "mom",
  "Lisa" => "sister",
  "Maggie" => "sister",
  "Abe" => "grandpa",
  "Santa's Little Helper" => "dog"
}

friends.each { |x| puts "#{x}" }
family.each { |x, y| puts "#{x}: #{y}" }


#2

family.each { |x, y| puts "#{x}: #{y}" }

x is the key and y is the value.

Method .each iterates through the family dictionary. In the first iteration x = "Homer" and y = "dad", in the second iteration x = "Marge and y = "mom" etc...

Every dictionary consists of key => value pairs. x will hold actual key and y will hold actual value.


#3

Hi I am a total newbie, just to clarify. According to what you mentioned above, if remove the Y, does that mean that it will purely just print the KEY instead? i.e. family.each { |x| puts "#{x}" }. However I tried removing Y, but it still print out both KEY and VALUE albeit in a different format. As shown in the picture


#4

Oh, I am deeply sorry, it was not my intention to mislead you.

Ruby is a pretty smart language. family is a dictionary. Every dictionary consists of key => value pairs.

When we execute this code:

family.each { |x, y| puts "#{x}: #{y}" }

Ruby knows that we want to divide a single element of the family into two parts - x and y. And this is pretty natural for a dictionary, x is key, y is value.

But in this situation:

family.each { |x| puts "#{x}" }

we say that we don't want to divide elements of the family. Undivided, single element of a dictionary is a mentioned before key => value pair. And this is what you see in your output, for example ["Homer", "dad"] is a single element of the family where "Homer" is a key and "dad" is a value.


If we want to print only the key we still need to divide our element into two parts, but use only the first one. So we need this code:

family.each { |x, y| puts "#{x}" }

Does it make sense now? :slight_smile:


#5

Hello, that said why does the output include the square bracket i.e. ["Homer", "dad"] ? Since undivided, single element of a dictionary is a mentioned before key => value pair, shouldn't the output be just "Homer", "dad" without the square bracket?
Also is the term dictionary same as hash? Anyway I like to thank you for your reply , it does help me to grasp a better understanding :>


#6

Comma separated strings is not a defined data type. They need a data structure. Hence, the array container.


#7

Also is the term dictionary same as hash?

Yes. When we say associative array, hash or dictionary we mean exactly the same thing.


#8

In the case of this code, why isn't the string contained by the array container?


#9

Because x is the key, y is the value. You can print x, you can print y, they are separate. They are plain values, so there is no need to put them in any data structure.

puts "#{x}: #{y}" means that we want to print out the value of x, a colon, and the value of y.


Here:

family.each { |x| puts "#{x}" }

x is a single element of the dictionary. We still need to assure that we will be able to retrieve key and value from it, so it cannot be something like "Homer dad", we need a data structure.


#10

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.