Hi All,
I’m taking programming lessons on Code academy, and I am stuck on the
symbols lesson.
The instructions say :
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 array, symbols.
Use .each to iterate over the strings array and convert each string to a
symbol, adding those symbols to symbols.
They give you :
strings = [“HTML”, “CSS”, “JavaScript”, “Python”, “Ruby”]
Add your code below!
Could someone please help me and explain to me why the code is written
the way it is?
Much thanks!
hachen
February 25, 2014, 4:59am
#2
So you want to take:
strings = [“HTML”, “CSS”, “JavaScript”, “Python”, “Ruby”]
… and create from it:
symbols = [:HTML, :CSS, :JavaScript, :Python, :Ruby]
The Array#each method [1] allows you to do something with each member of
strings, and the String#to_sym [2] method allows you to create a symbol
from a string. So roughly:
make a new array called symbols
for each entry in strings, create a corresponding symbol, and put it in
your new symbols array.
Hope that helps!
Sam
[1] http://www.ruby-doc.org/core-2.1.1/Array.html#method-i-each
[2] http://www.ruby-doc.org/core-2.1.1/String.html#method-i-to_sym
hachen
February 25, 2014, 5:24am
#3
Just wanted to thank everyone who replies to these emails. I learned
Ruby via forums and email lists, so I greatly appreciate your
generosity.
Best Regards,
Andrew