Problem with 1.9 Symbol Syntax (rocket has landed)


#1

This is my original post, which worked okay.

movies = {
    :"Fight Club" => "Thriller by David Fincher",
    :"Inception" => "Thriller by Christopher Nolan",
    :"O Brother Where Art Thou" => "Comedy-Drama by the Coen Brothers",
    :"The World's End" => "Comedy by Edgar Wright"
}

But when I try and change it to new syntax I'm getting errors. (I shortened to catch errors) This one even runs properly in Ruby itself, but not in code academy.

movies = {
     fight_club:"Thriller by David Fincher",
    inception:"Thriller by Christopher Nolan"
}

Error: Make sure to use the new hash syntax instead of the older hash rocket
syntax. (Clearly there is no rocket syntax.) I even ran the example from the question and it gave me this same error.

As strings

movies = {
    "fight_club":"Thriller by David Fincher",
    "inception":"Thriller by Christopher Nolan"
}

Error: Your syntax doesn't look quite right. Check the example in the instructions if you need help!

What's my problem?


9. Delete!
#2

I "solved the problem"

While I did nothing differently, I went back and retried some of the code from my above answers. The second set of code ran this time.

movies = {
     fight_club:"Thriller by David Fincher",
    inception:"Thriller by Christopher Nolan"
}

While before I was getting an error, now I am getting an "A OK!". What a waste of time and effort when I had the correct answer all along but was given an error message.

Word to the wise: Try it in Ruby as well. Had I done that in the first place I would have seen that my code worked and that Code Academy was giving me a false error.


#3

In the lab, this raises an error:

movies = {
    fight_club: "Thriller by David Fincher",
    inception: "Thriller by Christopher Nolan"
}

(eval):2: (eval):2: compile error (SyntaxError)
(eval):2: odd number list for Hash
    fight_club: "Thriller by David Fincher",
               ^
(eval):2: syntax error, unexpected ':', expecting '}'
    fight_club: "Thriller by David Fincher",
               ^
(eval):2: syntax error, unexpected ',', expecting $end

Which lesson is this? Please post a link. Thanks.


#4

It is for https://www.codecademy.com/en/courses/ruby-beginner-en-Qn7Qw/2/2?curriculum_id=5059f8619189a5000201fbcb

Now I get the "okay" for that code, but it prints this to the console-

{:fight_club=>"Thriller by David Fincher", :inception=>"Thriller by Christopher Nolan"}

This clearly has "rockets" inside of it. Perhaps that is meaningless to the exercise. When I print it in Atom I get:

{:fight_club=>"Thriller by David Fincher", :inception=>"Thriller by Christopher Nolan"}

Which again has rockets so I guess this is normal console-wise.

Are U getting an error with that code? This is the exact same code that 8 hours ago gave the error: Make sure to use the new hash syntax instead of the older hash rocket
syntax.


#5

I didn't get any error with the code in post #2.

As an aside, I'm not a big fan of fabricated names, but I do see how it can be an aid to efficiency. For proper names it follows that Title Case will be converted to snake_case and vice versa. Keys will start as a title case string made to a snake_case symbol, and will be returned as a title case string. This takes intervention.

user =  "The Princess Bride"

label = 'the_princess_bride'

key = label.to_sym

key is what the data stream would be using to create or access. Obviously we need a function between user and label.

Consider,

def titleize(x)
    words = x.split
    words.each do |word|
        word.capitalize!
    end
    words.join(' ')
end

user = "Fight Club"
label = user.gsub(" ","_").downcase!
puts label
key = label.to_sym
puts key
label = key.to_s
user = titleize(label.gsub("_"," "))
puts user

fight_club
fight_club
Fight Club
nil

Taking it one step further...

user = "Motorcycle Diaries"
label = user.gsub(" ","_").downcase!
#puts label
key = label.to_sym
#puts key
movies[key] = user
puts movies
label = key.to_s
user = titleize(label.gsub("_"," "))
puts user

Output

{:fight_club=>"Thriller by David Fincher", :inception=>"Thriller by Christopher Nolan", :motorcycle_diaries=>"Motorcycle Diaries"}
Motorcycle Diaries
nil

And it still passes.


#6

I'm not sure why I got the error then, but as I understand from your outputs, a variation of caps or non-cap (first letter) is allowed? Right? I see it in the titlesize function.

So then it must have been a glitch in the console or something? I mean, I never blame the console because 99.9% of the time it's not an bug, but maybe it was this time? What do u think?

This was such a simple question to get hung up on compared to the previous conversion one we worked on.

Anyway...when converting via the non-rocket, new method to hash symbols, how can U make the key side exactly what you want it?

In the old way I could do

:"Fight Club" => "Thriller by David Fincher",

With a string in the key, and thus the spacing a proper lettering. The new way seems to allow for none of this freedom. Is this just my perception? Is there way around it?

Is this just a reply to the overall questions methodology? Or to my specific names? Just wondering since we had to fabricate in this case.

Overall, thanks for the investigation. The case is solved, and I know exactly the syntax for Ruby 1.9 conversions. (The rocket, while much bulkier, is much preferred....by me)


#7

Not being an experienced Ruby programmer, I tend to rely alot on trial and error. My thinking on name creation leans to the human readable side, proper names versus symbolic. We would normally expect a user to enter "Fight Club" when searching for a movie. I've never entered anything like fight_club into a search bar.

Using a program to convert a Title to a symbol and back makes sense to me since it offers some consistency. This is not really fabrication if the machine does it. It is fabrication if we write it in a literal.

As far as writing a title as a symbol,

 > "Fight Club".to_sym
=> :"Fight Club"

We don't need to convert to snake_case unless we want to strip the quotes.

 > :"Fight Club".is_a?(Symbol)
=> true

This would suggest that we can write a symbol in a literal. It just doesn't seem like a human way to write data. That's where .to_sym comes in to work with human diction. We just keep on doing things the way that comes most naturally.


#8

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