New rubist with question

I’m learning Ruby via Codecademy, and the recent lesson was turning
strings into symbols. My instructions were:

We have an array of strings we’d like to later use as hash keys, but
we’d rather they be symbols.

Create a new variable, symbols, and store an empty array in it.
Use .each to iterate over the strings array.
For each s in strings, use .to_sym to convert s to a symbol and use

.push to add that new symbol to symbols.

I typed:

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

Add your code below!

symbols = []
strings.each do |s|

And it didn’t work. After researching, I found someone else with the
same problem who typed

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

Add your code below!

symbols = []
strings.each { |s| symbols.push s.to_sym }

Which did work. What did I do wrong, what am I not understanding?

And what is the difference between .each do || and .each {||}?

It does, thank you. That cleared things up quite a bit.

Just to elaborate a bit on your last question - the do/end block is
usually used when you have code that spans more than one line, like:

my_array.each do |item|

do something here

and something here


If your code only spans one line, you can use the curly braces instead
of do/end like this:

my_array.each { |item| puts item }

Hope that helps a little. :slight_smile:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs