It seems as though you are actually trying to make each string into a symbol. If this is the case, the running an each method will simply overlook any 's''s. To see what's actually happening in the working solution, let's break your solution down first.
strings = ["HTML", "CSS", "JavaScript", "Python", "Ruby"]
symbols = []
strings.each do |x|
#So, if x is equal to a Class String of S
if x =='s'
#Turn that x into a symbol, which none of them should work
x.to_sym!
symbols.push(x)
end
end
This is the code you say works.
strings = ["HTML", "CSS", "JavaScript", "Python", "Ruby"]
symbols = []
#Similar each loop, but they used s and not x
strings.each do |s|
#Instead of checking whether there is a Class String of S, it is just
#turning each value in the array into a symbol and pushing to the
#symbols array
s= s.to_sym
symbols.push(s)
end
Therefore, in summation, your code will never work unless there is an 's'
in the array, which there may be s's but not a single string with an s. The latter works because it changes them all without an if statement.
-edit-
I looked at the problem from Code Academy and I can see where the confusion came from, which is this instruction For each s in strings, use .to_sym to convert s to a symbol.
This is somewhat confusing pseudocode, but let me reformat it so you can see where the confusion occurred strings.each |s| { s = s.to_sym }
so the objective is to use s
as your empty variable. You did not need to detect s
, just use it as the empty variable.