11/16 - Placeholders and question on iterating


#1


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

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 }

**vs.**

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

puts languages.each { | | puts   }


#2

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 _ }

#3

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


#4

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


#6