11/16 - Placeholders and question on iterating



Just realized that one could add basically any variable as a place holder in order to iterate over the array. Believe it or not this was an aha moment for me.

Question: When I print scenario 1, the array prints twice. Scenario 2 (where there's a simple blank space in place of 'x', the array prints once. Any idea why this is?

**scenario 1:**
languages = ["HTML", "CSS", "JavaScript", "Python", "Ruby"]

puts languages.each { |x| puts x }


**scenario 2:**
languages = ["HTML", "CSS", "JavaScript", "Python", "Ruby"]

puts languages.each { | | puts   }


White space does not a placeholder make. Most printing characters that are not alpha (letters) will not work and likely throw an exception. It's common to see the _ (underscore) used in this setting.

languages.each { |_| puts _ }


Understood and will not use white space next time - though here it printed just fine. Any idea why the list prints twice v once?


There are two puts in the above. Remove the one at the beginning and it won't print twice.