Exercise 9/15 help


#1

Why don't this code work?

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

# Add your code below!
symbols = []
strings.each do |s|
   s.to_sym
   symbols.push(s)
end

I've checked for typos, and syntax. Don't understand what's going on.


#2

You'd have to combine both. Instead of:

s.to_sym
symbols.push(s)

try:

symbols.push(s.to_sym)

Good luck!


#3

Yes, that does work. It's weird that my way doesn't work though. I guess that's the Ruby way


#4

It doesn't work because by using the instruction s.to_sym, you're not modifying the s variable, you're only using it as a conversion to symbol and doing nothing with it.

If you want to do it separately from the push, you could use:

s = s.to_sym
symbols.push(s)

#5

Oh wow, thanks for this explanation. This makes a lot of sense to me.


#6

Yep, very good.
Strange that the console still returns them as strings tough, no?
["HTML", "CSS", "JavaScript", "Python", "Ruby"]


#7

Depends on your code. The ruby console in CodeAcademy returns what the last instruction evaluates to, so if the last instruction in your code returns the string array, then it will display the string array.


#8

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

symbols = []
strings.each do |s| s == s.to_sym
symbols.push(s.to_sym)
end
symbols

Just call "symbols"

output: [:HTML, :CSS, :JavaScript, :Python, :Ruby]


#12