Converting Between Symbols and Strings


#1

I haven't seen a topic solve this problem yet. Everyone seems to be completing the exercise without actually completing the task. The problem asks us to take each "s" that appears in the array and convert it to a symbol AND THEN push that "s" to the new symbols array. However, most people are just pushing every element in their entirety to the symbol array to pass the exercise.

So how do we check each letter and push each letter to the array?


Here is the cheaty code:

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

Add your code below!

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

print symbols


I would assume we would need to do something like
strings.each do |word|
word.each do |letter|
if letter == "s" || letter == "S"
symbol.push(letter.to_sym)
end
end
end

But this doesn't seem to work. Any insight?


#2

I too thought it was asking to convert every letter "s" to a symbol. However, one of the error messages I received stated "You didn't convert HTML into a symbol". Based off that message we can conclude it is not asking to convert all s's to a symbol. Maybe it's just using s as an abbreviation for string since the exercise is already using a variable named strings.

Also I was curious how to iterate through the array of strings and convert each given "s" into a symbol so I gave it try. I figured it out and it still gave me an error so that further confirms it's not looking to convert each s.

heres my code:

strings = ["HTML", "CSS", "JavaScript", "Python", "Ruby"]
symbols = []
strings.each do |word|
word.each_char do |letter|
if letter == "S"
symbols.push(letter.to_sym)
end
end
end
print symbols


#3

Ah I see. That makes sense, I suppose. I still wish they would have been more explicit in the directions... :no_mouth:


#4

my code
strings = ["HTML", "CSS", "JavaScript", "Python", "Ruby"]
symbols = []
strings.each do |name|
if string %"s" == "s"
symbols.push(name)
end
end
print letter
because not pass for the next, y not understand very this language program


#5

I'm not sure I understand what you're trying to say. Your English is a bit garbled. :confused:


#6

So here is the code that I used. The issue that I'm having is that I'm passing these exercises but not really understanding why lol.

strings = ["HTML", "CSS", "JavaScript", "Python", "Ruby"]
symbols = [ ]
strings.each do | word |
symbols.push(word.to_sym)
end
print symbols

My assumption is that the questions asks to take each of the strings in the strings array, convert each string to a symbol then those symbols will be inserted into the symbols array. When you print symbols instead of seeing "HTML" it is now :HTML as it is now a symbol and no longer a string.


#7

Thats a BINGO! You got it. You built an empty array, you iterated over the strings array, and then pushed the strings array into symbols. A+


#8

This code turns all the strings to symbols, and passes the lesson.

strings = ["HTML", "CSS", "JavaScript", "Python", "Ruby"]
symbols = []
strings.each do |word|
symbols.push(word.to_sym)
end
print symbols

#9

"s" is referring to string


#10

thats the key thing i'm just understanding after 30 minutes on this section :grinning:
ridiculous! heh heh