Help with to_sym and to_i


#1



https://www.codecademy.com/en/courses/ruby-beginner-en-0i8v1/0/4


Hi there,

This is not so much a problem with the code as with the understanding. If somebody could help me out understand what is going on here:

 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}."

...



#2

@chipjumper59596,

#Test if the given (=string= title converted to a symbol ) does not exist.....
puts movies[title.to_sym].nil?
if movies[title.to_sym].nil?
    puts "What's the rating? (Type a number 0 to 4.)"
    # get the =rating= as a =string=, later with =to_i= converted to integer
    rating = gets.chomp
    # now we add a new =property= to the =movies= hash......
    movies[title.to_sym] = rating.to_i
    puts "#{title} has been added with a rating of #{rating}."

References::
http://stackoverflow.com/questions/885414/a-concise-explanation-of-nil-v-empty-v-blank-in-ruby-on-rails
http://stackoverflow.com/questions/8379596/how-do-i-convert-a-ruby-hash-so-that-all-of-its-keys-are-symbols
http://stackoverflow.com/questions/2341837/understanding-symbols-in-ruby


#3

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