Converting Between Symbols and Strings


#1


https://www.codecademy.com/courses/ruby-beginner-en-Qn7Qw/1/4?curriculum_id=5059f8619189a5000201fbcb#


It says there that "create a variable called symbols, and store empty array on it," so I did follow that. Then on the second instruction, it says that "use .each to iterate over the strings array," Then on the third instruction, it says that "for each s in strings, use .to_sym to convert s to a symbol and use .push to add the new symbol in the symbols.

So, what I think of s is the strings stored in the strings array. So, I have to ask what is wrong about the code. It says that the "It looks like HTML isn't a symbol," and I also noticed it on the other arrays, it doesn't convert into a symbol.

Or, I misinterpreted the s in the instructions.

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

# Add your code below!

symbols = []

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


#2

String.to_sym is not an in-place method. Either assign the new value to a variable, or pass it directly to the push() method.


#3

you have to use symbols.push(s.to_sym) like mtf suggests. I have no clue how this works lol.

Would you mind expanding on how .to_sym works when passed directly into push, and not stand alone like OP used?


#4

String.to_sym is not an in-place method, as we stated earlier. To preserve its effect we need to assign it.

symbol = s.to_sym

Now symbol is the cached value.

Array.push() takes an object and inserts it at end of the array (appends it).

symbols.push(s.to_sym)

takes the output of the method and assigns it directly to the array, the same as,

symbols.push(symbol)

given the above cached value.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.