I’m learning Ruby via Codecademy, and the recent lesson was turning
strings into symbols. My instructions were:
We have an array of strings we’d like to later use as hash keys, but
we’d rather they be symbols.
Create a new variable, symbols, and store an empty array in it.
Use .each to iterate over the strings array.
For each s in strings, use .to_sym to convert s to a symbol and use
.push to add that new symbol to symbols.
I typed:
strings = [“HTML”, “CSS”, “JavaScript”, “Python”, “Ruby”]
Add your code below!
symbols = []
strings.each do |s|
s.to_sym
symbols.push(s)
end
And it didn’t work. After researching, I found someone else with the
same problem who typed
strings = [“HTML”, “CSS”, “JavaScript”, “Python”, “Ruby”]
Add your code below!
symbols = []
strings.each { |s| symbols.push s.to_sym }
Which did work. What did I do wrong, what am I not understanding?
And what is the difference between .each do || and .each {||}?