Forum: Ruby Beginner question

A1a03140db9ef9e21d770c8bbcf7757e?d=identicon&s=25 Mei Chen (hachen)
on 2014-02-25 04:16
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!
Caf8121e4a2582b3844a83df64638493?d=identicon&s=25 Sam Duncan (Guest)
on 2014-02-25 04:59
(Received via mailing list)
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#met...
44ca9d43f036568dc061c9168c68d297?d=identicon&s=25 Andrew Kelley (Guest)
on 2014-02-25 05:24
(Received via mailing list)
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
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.