Beginner Stuck On Tutorial

Hi all,

I’m sure this is really easy but I’m having trouble working out how to
complete a Ruby tutorial converting strings to symbols.

The idea here is to convert the strings array to symbols using .to_sym
and to push them into a symbols array. They are not converting though.
Can someone please point me in the right direction?

strings = [“HTML”, “CSS”, “JavaScript”, “Python”, “Ruby”]

symbols = []

strings.each do |language|
language.to_sym

Mark McNaughton wrote in post #1154045:

The idea here is to convert the strings array to symbols using .to_sym
and to push them into a symbols array. They are not converting though.
Can someone please point me in the right direction?

You’ve got the right idea, what you haven’t done is “push them into a
symbols array”.

strings = [“HTML”, “CSS”, “JavaScript”, “Python”, “Ruby”]

symbols = []

You can push one symbol into the array using: symbols << :html

strings.each do |language|
language.to_sym

Does that hint let you complete your program?

You should also look up, or experiment with, the ‘map’ method…

HTH,

Peter.

Hi Peter, thanks for your reply.

For some reason my pasted code did not paste fully. What I have is
this:

strings = [“HTML”, “CSS”, “JavaScript”, “Python”, “Ruby”]

symbols = []

strings.each do |language|
language.to_sym
symbols.push(language)
end

The tutorial says to use the .push method. The warning message I get
with this is “It looks like HTML isn’t a symbol.”

The to_sym method converts a string into a symbol and returns it, but it
does not modify the string itself. Currently your language variable
still points to a string and the symbol is returned to nowhere.

You can do either:

symbols.push(language.to_sym)

or:

symbol = language.to_sym
symbols.push(symbol)

Thanks Matze, I think I understand now. I believe the tutorial was
indicating that your first suggestion was what it was looking for.

Many thanks!