FAQ: Hashes and Symbols - Converting Between Symbols and Strings

This community-built FAQ covers the “Converting Between Symbols and Strings” exercise from the lesson “Hashes and Symbols”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Ruby

FAQs on the exercise Converting Between Symbols and Strings

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

If I run the initial code even before adding to it, it gives out warning:

(test):10: warning: found = in conditional, should be ==

How crucial is it?

1 Like

Not the best of instructions for this exercise; in fact they were a bit misleading. In step 3, instead of saying:

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

They should’ve said for each ‘s’ push it to symbols and then convert each string to symbol using .to_sym

I was getting the same error as you.

4 Likes

I’m getting the same error as well. I have

strings = [“HTML”, “CSS”, “JavaScript”, “Python”, “Ruby”]

Add your code below!

symbols =
strings.each do |s|
symbols.push(s)
s.to_s
“s”.to_sym
end

puts symbols

I tried to push it first and get the same error?

The conversion to symbol should take place before pushing it to the symbols array.

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

I don’t believe the instructions are misleading, but I don’t like the format of the .each used in the example. What I wrote and succeeded with is below.

symbols = []

strings.each { |s| symbols.push(s.to_sym) }

print symbols
4 Likes

Why ‘hate’ when they are both valid. Learners should know how to do both, and not get fixated or biased toward one in favor of another. We start them out with do..end, and let them get comfortable with the syntax, then work them into block syntax. I’m sure that’s how I learned this stuff, way back when.

3 Likes

I’m merely stating my preference. If ‘hate’ is too strong a word to use here, I have edited my post.

1 Like

I was struggling with this exercise too as I didn’t know you would have to create another variable that stores the strings to symbols first and then actually use the variable to push.

1 Like

it’s an intermediate step to help see the process. There is no ‘have to’, though.

symbols.push(s.to_sym)

or even,

symbols << s.to_sym

The latter can only be used when pushing a single value. The formal method, .push() can take one or more arguments which all get appended to the array.

5 Likes

why t = s.to_sym? is t arbitrary?

No, t is not arbitrary. No variable name is arbitrary. You can use names that make sense to you. The line you referenced came from this example from @mtf:

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

This would work just as well:

strings.each do |my_string| #these names are more descriptive, but also more of a pain to type repeatedly
    my_symbol = my_string.to_sym
    symbols.push(my_symbol)
end

This would also work, but looks ridiculous, and makes it harder to understand what the function is doing:

strings.each do |blinker_fluid|
    muffler_belt = blinker_fluid.to_sym
    symbols.push(muffler_belt)
end

All three examples do exactly the same thing. The names are up to the programmer as long as they are legal variable names. It is best to use names that make it easier for readers of the code to understand what is happening. In a simple function like the one above, short single letter variable names are fine because the actions of the function are very evident (the code is self explanatory).

1 Like

If we mean freely chosen then arbitrary would be one word to describe it. While the term may mean random or capricious, we give meaning according to our own design, and randomness does rather come into play. At any length, the names we choose to use may be descriptive or generic, depending on where they fit in.

I use simple variables when there is little or no need for verbose description. The code describes itself.

In the broader program we will have descriptive names for the main moving parts but not every variable needs to be named this way. For basic utility methods, simple is sufficient enough, and generic names lend themselves to universal functions.

Fg = k (m1 * m2) / d ** 2

Fe = k (q1 * q2) / d ** 2

Do we need two dedicated methods to solve these very similar equations? Not really. A basic generic function will do the work, and the terms we use outside of the method will provide description.

Fg => Force of attraction due to gravity
Fe => Electrostatic Force of attraction due to the charge on two particles

k  => constant of proportionality (Newton's or Coulomb's, respectively).
m  => mass
q  => charge
d  => distance
def calc_Force (k, a, b, d)
    (k * a * b).to_f / d ** 2
end
1 Like

t is arbitrary, but im pissed that they never taught us to store these changing values in another one or to even think like that… i NEVER would have figured this out in a million years without looking at this answer

4 Likes

I got the same error, even when my code worked properly - it just tacked it onto the end of my printed array.

I played around and realized that if I move my symbol array above the # Add your code below! comment, the error message goes away. Must have something to do with how it’s evaluating our code. It’s only expecting comparison operators, not an assignment operator after the comment for some reason.

6 Likes

Actually since you can call the arguments anything you want the instructions are misleading, I thought they were asking for something completely diferent. Just say to convert each string in the array in to a symbol and append it in a new array, that should be easier to understand

1 Like

Same here. The explanation for to_sym at the top of the exercise used a string and suggested that to change the values of s to symbols would be “s.to_sym”. To me at least. Nowhere did I see any suggestion that you would need to create a new variable.

And yet if you are doing it in a block [as in { |s| symbols.push(s.to_sym) }] you don’t need to create the variable. Could anyone explain why this is? This was the initial code I tried:

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

I’m guessing that it doesn’t work because s.to_sym is meaningless, but the example of changing a string to a symbol suggests that it would be the same for whatever s is in this case, doesn’t it?

2 Likes

Not meaningless, as much as 1 + 2 is not meaningless, just without binding. It is an expression, which is just a value. Values don’t do anything, but neither are they meaningless once they are assigned or used as an argument.

symbols.push(s.to_sym)
3 Likes

Thanks! I get it now. Untethered. All at sea. Moving on . . .

1 Like

No, I believe the way they worded it was perfectly clear. In order for a function to execute on a value, any expressions inside must be evaluated first, so they go from inside to outside. So first you type s.to_sym and then you put that inside symbols.push(). The way you worded it would mean that you push it first, and then convert it, would it not? In that case, it would mean

...
symbols.push(s)
s.to_sym
...

I understand what you mean because you could type it left to right, but giving instructions to Ruby would go inside to outside as in symbols.push(s.to_sym) or top to bottom, as in

s = s.to_sym
symbols.push(s)

Which does the same thing as the shorter statement I already wrote.
If you print out strings at the end of the program, you will see that it has not been modified by the code above, making that code a valid solution.

As for the = in conditional error, I also got it, but running the code again to check my assertions in this post made it go away. :l

1 Like