Converting between symbols and strings


#1

Can someone explain to me what each s in strings is?
and have we even learned the .push method yet? I've been taking pretty good notes and don't recall. Also, why does this work because it didn't convert anything to symbols if that was the point of the exercise. Plz give me a simple play-by-play, i beg of you

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


#2

S in strings is each character in the string. You learn push here. .to_sym makes it into a symbol.


#3

s is each single string in the array strings. And it need not necessarily be named s. It can be named anything you like. Example:

strings = ["HTML", "CSS", "JavaScript", "Python", "Ruby"]
strings.each do |unicorn|
  print unicorn + ","
end
# => HTML,CSS,JavaScript,Python,Ruby,

(run code)
(@cadecodes you missed the above point)

.push is used to insert new elements into a array. You must have learned this in a previous unit.

And, your code does work. You'll see the output if you print the symbols array.

Hope it helps!


#4

I agree, .push is introduced here for the first time.


#5

Thank you guys. Got in some real trouble on this one... Solved!


#6

Thanks guys.
I got so confused on this one!
I thought it was asking to take the letter 's's out of the strings [like "CSS"]. convert them to symbols and shove them in the symbols variable.

I was like @_@;;; oh god, when did we learn to do this?! lol


#7

Compared to say JS or C++, really feel like this Ruby course is really messily taught. There's not a clear understanding or explanation of the things that it introduces, so it becomes more of "put in the right code to pass" rather than "understand the code and pass."

Looking through the forums, it seems like a lot of people are running into this same thing, a lot more are asking more of what code is acceptable rather than why it is.


#8

Thats what I thought too! :fearful: Luckily it was a lot simpler...


#9

Hey, could someone please tell me what's wrong with my code?

strings = ["HTML", "CSS", "JavaScript", "Python", "Ruby"]
symbols = []
strings.each { |s|
s.to_sym
symbols.push(s)
}


#10

You need to re-assign the result of s.to_sym back to s.