9. Converting Between Symbols and Strings


#1



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


So when I write code it gives me this result: ["HTML", "CSS", "JavaScript", "Python", "Ruby"]
And this error message: "Oops, try again. It looks like you didn't add each symbolized string to the symbols array."
I'm really stuck, tried a lot of possible options, but none works. Please help =)


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

# Add your code below!

symbols = Array.new
strings.each do |i|
    if i == "s"
        symbols.push(i.to_sym)
    else
       puts "something went wrong"
    end
end.


#2

 if i == "s"
        symbols.push(i.to_sym)

you have to convert all the string values to symbols. so you don't need any condition.


#3

Okay, so I've written only this:

symbols.push(i.to_sym)

and it works, but the result is still the same. shouldn't it return only CSS and Javaschirpt?


#4

Why would it only return those two?
You've told it to convert all strings in the array into symbols and pushed them into another array.


#5

Instruction:

For each s in strings, use .to_sym to convert s to a symbol and use .push to add that new symbol to symbols.

here s is just a placeholder name. you used i instead of s .

so each i in strings, use .to_sym to convert i to a symbol.

I don't think so.because that time your if condition was false so it didn't convert any strings to symbol. i can't be 's'. first of all consider what is i .

i hope you've understand now :slight_smile:


#6

Hi there, may someone help me please .. I wrote something that seems right but I don't get why I have that asnwer:

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

And the answer is: ["HTML", "CSS", "JavaScript", "Python", "Ruby"]

It's false right ? I'm supposed to convert those strings into symbols .. aaarg :grimacing:
Thanks guys !


#7

symbols NOT symboles :slight_smile:


#8

Yes, now I understand. No I just thought, that in variable symbols should only go words that contain letter "s". Now I get it, thanks a lot.


#9

This is actually irrelevant, as it's a variable name. He just happened to write it in French (which I tried, and it worked, despite instructions).

Anyway, the code is correct – but the console doesn't output symbols. Not sure why.


#10

if you put, print symbols at the end, you'll see the strings did turn into symbol, hope that helps


#11

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