Converting symbols and strings 9/15


#1



Hi Everyone,
I have a question. I've been having trouble with the 9/15 exercise in Ruby "converting Symbols and Strings
I've been looking for an explanation in the already existing topics and found pieces of code that worked for people and so i tried them all but still i couldn't pass the exercise. The symbols wouldn't appear in the console. Most of the time, the blocks of code would be something like that:

symboles = []
strings.each do |s|
symboles.push(s.to_sym)
end

At some point, i had the idea to create a variable symbols that would be equal symbols.push(s.to_sym), like that:

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

And it worked, i passed the exercise but the symbols still won't show up in the console. So according to code academy, i've passed, but i still don"t have the result i was expecting.
Anyone has any idea why?

Please let me know if you need more info or if there's something unclear

Replace this line with your code.


#2

It very likely passed because the lesson checker saw the correct pattern, and ignored the unseen assignment. That line is not valid code since it assigns a statement to a variable. We need a function to be able to do that.

Your first example looks correct, and is the valid code.

Aside

Did you mean to write, symbols as the variable name?


#3

The first example looks correct to me too, and apparently it was the right answer for every people who had trouble with the exercise, but for me it sent back an error message

my variable name is symboles because i'm writing in french. Is that what you're refering to?

Thank you for your answer


#4

Is it the english track, though? We should use the variable names given in the instructions, for best results.

Please post the error message. Thanks.

Also, for reference, a link to the exercise so we can test. Thanks.


#5

yeah the instructions were in french

it wasn't an error message so to speak but in the console i only had the strings (and no symbols) and at the bottom of the screen a message like "oops something went wrong, you didn't put symbols in the array" or something like that. I can't really show you cause it won't be displayed anymore now that i have passed... Plus, now i've written the first code (without the variable symboles) and it passes too. (still without showing the symbols on the console, though)

https://www.codecademy.com/fr/courses/ruby-beginner-fr-FR-t5bz1/1/4?curriculum_id=535e93005d81962043000001


#6

Ah, that makes a difference. Merci!

For the life of me I cannot explain the forced output. But then...

strings = ["HTML", "CSS", "JavaScript", "Python", "Ruby"]
# Ajoutez votre code ci-dessous
symboles = []
strings.each do |s|
    symboles << s.to_sym
end
symboles.each do |x|
    puts x
end

HTML
CSS
JavaScript
Python
Ruby
[:HTML, :CSS, :JavaScript, :Python, :Ruby]

Adding a slight change...

symboles.each do |x|
    puts x.is_a? Symbol
    puts x
end

true
HTML
true
CSS
true
JavaScript
true
Python
true
Ruby
[:HTML, :CSS, :JavaScript, :Python, :Ruby]

Scratching my head on this one.

Stick this in at the very bottom (of the code)...

puts symboles

and the output is,

true
HTML
true
CSS
true
JavaScript
true
Python
true
Ruby
HTML
CSS
JavaScript
Python
Ruby
nil

#7

It would appear that Ruby has some nuances of its own in this regard. Without that last line, at least we get to see the structure left over in the buffer. Visual confirmation says they are symbols.

We could try to construct a hash and see what that reveals.

strings = ["HTML", "CSS", "JavaScript", "Python", "Ruby"]
symboles = []
strings.each do |s|
    symboles << s.to_sym
end
languages = Hash.new
symboles.each do |x|
    languages[x] = x.to_s
end
puts languages

{:HTML=>"HTML", :CSS=>"CSS", :JavaScript=>"JavaScript", :Python=>"Python", :Ruby=>"Ruby"}
nil

languages.each do |k, v|
  puts "#{k}: #{v}"
end

HTML: HTML
CSS: CSS
JavaScript: JavaScript
Python: Python
Ruby: Ruby
=> {:HTML=>"HTML", :CSS=>"CSS", :JavaScript=>"JavaScript", :Python=>"Python", :Ruby=>"Ruby"}

#8

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