Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAlphonse Cuccurullo
2,513 PointsIm trying to convert to_sym.
So i know this is totally wrong but im stumped. Trying to iterate over a array and make them all symbols.
arr_num = [1,2,3,4]
arr_num.each_index do |ind|
print ind.collect {|co| co.to_sym puts co}
puts ind
end
1 Answer
Maciej Czuchnowski
36,441 PointsIf you just want an array of symbols made from this, you can write:
arr_num.map {|num| num.to_s.to_sym }
or a shorthand version:
arr.map(&:to_s.to_sym)
You can't convert numbers directly into symbols - you have to turn them into strings first.
Alexander Davison
65,469 PointsNice answer!
Alexander Davison
65,469 PointsAlexander Davison
65,469 PointsHi Alphonse Cuccurullo,
Can you please format your code properly so people can debug you code?
If you do this:
name = "Alex"
There won't be any formating. If you do that for multiple lines, it is unreadable.
However, if you do this instead:
```ruby
name = "Alex"
```
it will look like this:
name = "Alex"
Please use formatting when posting code.
Thanks! ~Alex