Symbols.push(s)


#1

Hi,

I have the exact same code as the one at the top of the page. I tried changing the

symbols.push(s)

to symbols.push(s.to_sym)

but this did not change anything. I cannot follow your examples on the bottom in relation to this because I am too inexperinced to make the connection.

Can U explain how to write the code specific to this question? The hint that they give has the exact same syntax as the code specified by the original poster of this thread, and so I came up with the exact same answer as that person did. It does not refer to ! anywhere in the text.

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


Is it possible to write the action in two steps
#2

Because it does not apply in this problem. The OP was attempting to write a symbol back onto itself, which as we've mentioned can not be done. I gave an example of how it can be done with a few other methods, just to illustrate what in-place mutation meant and how it is done in those cases.

Please show us the code you have so far and we can examine it for possilbe errors.


#3

I'm just super unfamiliar with the mutation occurence. The question says to use "s" in strings.

My code is

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

symbols = []

strings.each do |s| 

s.to_sym

symbols.push(s)
end

print symbols

I get the error "Oops, try again. It looks like HTML isn't a symbol."
Why?

The syntax provided in the example is
numbers.each do |number|
if number % 2 == 0
evens.push(number)

It seems that this is exactly what I did, except for the "if" which I tried and it made no difference.


#4

mutation means change in character, such as case change, type change, etc. Don't get too hung up on this.

Any time we convert something to something else, we are mutating it.

float.to_i

int.to_f

number.to_s

string.to_sym

All of these illustrate mutation. Some float is converted to an integer, an integer is converted to a float, a number is cast to a string, and a string is cast to a symbol ('cast' and 'convert' are synonymous terms in this sense). They all need to be assigned to a variable, or passed as an expression in method call.

s.to_sym

This line is not perfoming the conversion we expect. I ran your code and then polled the symbols array.

 HTMLCSSJavaScriptPythonRuby
=> nil
 > symbols[0].is_a?(Symbol)
=> false

Now change these lines,

s.to_sym
symbols.push(s)

to

symbols.push(s.sym)

and run again.

 > symbols[0].is_a?(Symbol)
=> true

This is possibly a problem since it is not asked for in the instructions. The correct way to print an array is the same as what we used to create it. array.each

symbols.each {|x| puts x }

#5

So even though I cannot write the code you are writing to check for the symbols, I get what U are doing and see how the first set is not creating symbols, while the second

 > symbols[0].is_a?(Symbol)
=> true

is.

But now I've changed the code to this,

 > symbols = []
strings.each {|s| puts s}    ## also tried with strings.each do {|s| puts "{#s}"}
symbols.push(s.sym)
end

I'm getting an error, "Oops, try again. It looks like you didn't add each symbolized string to the symbols array."
I'm also getting an error on the "end" as well, so I just erased it and still the code did not work.

I appreciate all your responses. I am not sure how I can possibly answer this based on what I have learned so far in this course. Since I have done all the lessons previous to this, and I still cannot figure this out, I guess I'll need to consult some other resources.

Any other advice on how to go about this?


#6

I gave up an went for the answer
It needed a piece in between, a variable that held the string converted into symbol.
So now I have:

strings.each do |x| 
sym = x.to_sym
symbols.push(sym)

Along with the intial array and end. And it works


#8

Should this maybe have been

symbols.push(s.to_sym)

I got it to work with the second one.

It would make alot of sense if you made a typo and I was not savvy enough to catch it, and that was my problem, otherwise I just could not get that to work. Anyway, thanks!!!!!


#9

You were almost there!

symbols.push(s.to_sym)

Don't give up when help is already present and you are making headway. Study the problem from an objective point of view. What is expected? How might we plan to fulfil that objective? What hurdles present?

Notice that we go right back to there having to be an assignment, or an argument.

s = s.to_sym
symbols.push(s)

The above is an assignment, which variable then becomes the argument.

symbols.push(s.to_sym)

is passing the expression as the argument. Same outcome, either way.

Strongly recommend that you give this topic a good review and practice on the side. There are things here that really need to be cemented. The concepts are either tools or stumbling blocks. Best make them tools.

Programming is about tackling problems. That's why we write programs. To perform a larger task that cannot be easily sought after in a glossary or answer key, forum, etc. Only our code can find the answer we seek. That's what having a clear objective is all about, and only we can master that art. It has to be us that does it.


#10

Yeah, it's true.

In the future I'll move on without answering it, and then return back to later, until I can answer it in my own,

This time I just got too frustrated.

Thanks for all the explanation once again,


#11

Yes! Now you're getting somewhere. You're spotting my typo's. Fantastic, and my apologies. That was not intentional.


#12

The way this course is laid out, I wouldn't skip anything. Stick with it until you are comfortable with what's presented. Only people with lots of coding experience can breeze through this course. Give yourself a chance to breeze through the later modules by mastering the early ones.

Which as it would turn out is a good sign. It means you are pouring over it. We all fail in so many ways but as long as we are staying with the problem (a break from it is good, and recommended, just come back to it) we will build upon the foundations of our earlier efforts and learn as we go.

Every problem begins the same way. We need a foundation. Rejoice in your frustration, but go easy on yourself. Begin to see it as reinforcing. Once we get frustrated we call in the big guys. That's the part of our brain that doesn't get engaged without some effort on our part.


#13

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