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 trial

Ruby

Alphonse Cuccurullo
Alphonse Cuccurullo
2,513 Points

Someone help?

Syntax

movies = { "titanic" => 7,
           "pokemon" => 8,
           "digimo" => 9
       }

puts "Choose one of these\n\t"
puts "Hit add if you wanna 'add' a movie\t\n"
puts "if you wanna delete a movie hit 'delete'\t\n"
puts " Wanna update rating?\t\n"
puts "wanna display the movies? If so hit display"


choice = gets.downcase

case choice
when "add"
  puts"Please add a movie."
  title = gets.chomp
  if movies[title.to_sym].nil?
    puts "What's the rating? (Type a number 0 to 4.)"
    rating = gets.chomp
    movies[title.to_sym] = rating.to_i
    puts "#{title} has been added with a rating of #{rating}."
  else
    puts "That movie already exists! Its rating is #{movies[title.to_sym]}."
  end
end

Link: https://www.codecademy.com/courses/ruby-beginner-en-0i8v1/0/5?curriculum_id=5059f8619189a5000201fbcb#

So i know i did this code wrong something with the to sym im doing wrong. So asside from that issue the other is that im clueless on combining the vars. Like for example the movies[title.to_sym] = rating.to_i. This part completely loses me if you can id like this explained in detail. Like how would i know when to combine something like this, is there a name for this technique.

Hi Alphonse Cuccurullo could you tidy up the code snippets, please? Check out the Markdown Cheatsheet if you're not sure how to do it. It's really hard to read at the moment. I could do it myself as a moderator but I'm getting tired of asking people! I think Treehouse sorely need to make it more obvious how to do it.

I've done it

1 Answer

movies[title.to_sym] = rating.to_i

First of all, after reading this, I recommend you open irb in your Terminal and have a play around with some hashes. Using irb is a really important habit to get into - it allows you to quickly experiment with things. I do it all the time at work - it's not just something for amateur devs.

The above line of code will insert a key of title.to_sym with a value of rating.to_i into the movies hash.

title was captured with gets, which means it will be a string. to_sym converts it to a symbol.

rating was also captured with gets, which also means it will be a string. to_i converts it to an integer.

So, let's say the user entered, when prompted, goodfellas and 5. After that action, title == "goodfellas" and rating == "1".

After the code movies[title.to_sym] = rating.to_i is run, the movies hash will look like:

movies = { "titanic" => 7, "pokemon" => 8, "digimo" => 9, :goodfellas => 5 }

Probably not good to have some keys as strings and some as symbols, but this is what will happen with the code you presented.