FAQ: Hashes and Symbols - Converting Between Symbols and Strings

From context (for example the fact that you are using the .each method), the instructions made it rather clear that the lesson is asking you to name the current item being processed as s, then use that value in your code.

Also, the code makes it easier for learners to understand what code they need to write, instead of the lesson prompting them to figure it out themselves. I suppose that plain statements meaning “make it do this” would be fine for later lessons, but it does help introduce the concept and how to do things in this lesson.

Why doesn’t this work? I’m getting that (test):10:warning

symbols =

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

Needs to be assigned to a variable.

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

or, written directly into the push() argument,

strings.each do |string|
  symbols.push(string.to_sym)
end
2 Likes

Hi mtf !

I have 2 questions for you please :slight_smile:

I don’t understand what is t in your code ? A variable for sure but can’t we just do s.to_sym to convert those as a symbol ? If not, why ?

My second question, here my code :

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

# Add your code below!

symbols = []

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

print symbols

It works thanks to you but I have an error in the console only at the first run :

[:HTML, :CSS, :JavaScript, :Python, :Ruby](test):10: warning: found = in conditional, should be ==

Do you know why ? Thanks !

We can, if it is used directly as the argument to push().

Can’t explain that other stuff. It doesn’t apply to the code above.

1 Like

Awesome ! It worked like this, thank you :slight_smile:

strings.each do |s|
  symbols.push(s.to_s)
end
1 Like

I couldn’t figure out why my code wasn’t working so I reset the entire exercise and got the solution. Even the Codecademy code got the exact same error as many others have (myself included):

:HTML, :CSS, :JavaScript, :Python, :Ruby:10: warning: found = in conditional, should be ==

1 Like

When would I want to convert a symbol to a string?

When printing or embedding in other text, would be one time.

1 Like

To throw my two cents into the do vs. { } debate… I remember in something else I was reading about Ruby… the do syntax is for when you know you’re going take up multiple lines and { } is for when its a one liner kind of deal. At the end of the day we all have different speech patterns right? For Example: the Do is maybe : Please give me some orange juice and the { } is OJ Please. :tangerine:

The instructions are quite succinct. What issue do you have?

apologies i see the question properly now haha

1 Like

I am getting more frustrated with the instructions in the exercises as I progress through this course. I rack my head over some problem, and once I look at the solution realise they were asking for something else and worded things in a weird way.

The easiest way to deal with frustration is to ask questions rather than gripe about the deficiencies of the course. As we’ve witnessed in this thread, once we learn to see things in a different way (other than the way we expect to see them) then clarity ensues. Keep an open mind and be responsible, thereby learning to be responsive and accepting of alternative ideas and solutions.

Thanks for the advice. I apologize for the griping.

1 Like

This is the answer. thank you.

why doesn’t “puts (“beans”.to_sym)” print “:beans” to the console?

It looks like puts and print call the to_s method on the symbol, rendering it as string data. Can’t really explain it, but if you push the symbol to the symbols array, and print it, you’ll see it is definitely made into a symbol.

1 Like