Converting between symbols and strings


#1

I'm doing Converting between symbols and strings and I'm iterating through the strings and converting them to symbols and when finished I get an error! and I don't understand what I did wrong!
`strings = ["HTML", "CSS", "JavaScript", "Python", "Ruby"]

Add your code below!

symbols = []

strings.each do |s|
s.to_sym
symbols.push(s)
end
end`


#2

after your .each you should use a for loops like that...

strings.each do |s|
    for s in strings
        symbols.push(s.to_sym)
    end
end

#3

hi wizmarco
pspmasters code seems just as correct to me (I am new to this). Why is your code right? Could you please explain why yours is correct and his is not? I'd really appreciate it.

thanks!

redheadTed


#4

It is supposed to be done like so:

strings = ["HTML", "CSS", "JavaScript", "Python", "Ruby"]

# Add your code below!

symbols = []

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

Converting the string to a symbol within the push method. Hope that helps!


#5

Here is another way to achieve this:

s trings = ["HTML", "CSS", "JavaScript", "Python", "Ruby"]

# Add your code below!

symbols = []
strings.each { |s| symbols.push(s.to_sym)}

As for "why" it works, in the original example, I don't think the OP is pushing the s.to_sym into the new array symbols. He's just pushing the variable "s"

In mines and the others, we are all pushing the conversion of the "string" to "symbols" to the new array and thus, only symbols are present in said array instead of strings.