7.Update


#1

Just so you know, I understand the given task in this exercise, my code will prove it as well.
Here's my 'update' block:

when 'update'
    puts 'Which movie would you like to update?'
    title = gets.chomp
    if movies[title] = nil
        puts 'This movie doesnt exist!'
    else
        movies[title.to_sym]
    puts 'What should his new rating be?'
        rating = gets.chomp
        movies[title.to_sym] = rating.to_i
    puts 'Done!'
end

The reason I've created this topic is to understand how Ruby ( or own self as well ) understand the if\else statement.
If I'm following the 'else' statement in this block, it does it is suppose to do.
But if I'm following the 'if' statement, it totally ignore to check if the movie does exist!

I've been able to get past this exercise, this one scratch my head big times..


#2

your line of code for the if statement should be like this:

if movies[title.to_sym].nil?

That should do the trick


#3

Also, the code write under your else: movies[title.to_sym] should be removed


#4

2 questions:

-Where's that .nil? come from? And how does it acts in Ruby?

-Why should i remove the mentionned else statement too?


#5

1) The .nil? basically asks Ruby if the movie that has been entered exists, if it does not exist it will run the code below, if it does it will move down to the else part of the if/else statement.
If I remember correctly from the exercises, nil is the Ruby way of saying nothing.

2) Because, when you start your if statement, you have already converted the user's input to a sym, so when it comes to the else part of the statement, the movie entered has already been converted and found in the system. All the else part of the statement has to do, is run the code you want it to do, in this case get the rating, convert it and print out a piece of string.

Hopefully that makes sense, I am no pro by all means, I just understand this exercise very well.


#6

Actually, that was pretty well explained!
Thanks a lot for your time, man! :smiley:


#8