If you run my Ruby code

If you run my Ruby code what happens ? for me it seems to work with no errors, but it isn’t actually doing anything.

For example, if I add a movie then do display, the movie is not added. same for update and delete.

Night at the Movies.rb

Hello @paulgureghian, could you please post your code, as when we follow the link, we get our personal editor:
Press this button:

And put your your code in between the back ticks:
Screen Shot 2020-05-02 at 14.06.13

#!/usr/bin/env ruby
# frozen_string_literal: true

# Created by Paul A.Gureghian in May 2020. #
# This Ruby program keeps track of movie ratings. #

# Start of script. #

#  Create a hash to hold movies / ratings. #
movies = {
  Alien: 10.0,
  Aliens: 9.0  

puts "\n"
puts 'This program allows you to add, update, display and delete movies from a database.'
puts 'Enter "add", "update", "display", or "delete":'
choice = gets.chomp

# Create a case statement. #
case choice 
when 'add'
  puts 'Add a movie:'
  title = gets.chomp.to_sym
  if movies[title.to_sym].nil?
    puts 'Rate the movie:'
    rating = gets.chomp
    movies[title.to_sym] = rating.to_i
    puts "That movie already exists and its rating is: #{movies[title.to_sym]}."  

when 'update'
  puts 'What movie do you want to update?:'
  title = gets.chomp  

  if movies[title.to_sym].nil?
    puts 'That movie does not exist.'
    puts 'What is the new rating?'
    rating = gets.chomp
    movies[title.to_sym] = rating.to_i 


when 'display'
  movies.each do |title, rating|
    puts "#{title}: #{rating}"
when 'delete'
  puts 'Enter a movie title to delete:'
  title = gets.chomp
  if movies[title.to_sym].nil?
    puts 'That movie does not exist.'

  puts 'Error: Please enter "add", "update", "display", or "delete" into the prompt.'
# End of program. # 

The problem is once you do something, the code stops running, that way any information you added gets forgotten again. You should use a loop to keep the data, or at lest print the hash after each instruction.

keep = "y"
while keep == "y"#While the variable is 'y', do this:
  #code for the hash
   puts "Keep going? Type 'y'"#asks if the user wants to continue
   keep = gets.chomp#resets the keep variable to the user's choice

I hope this

Print the hash after ‘add’ , ‘update’ , ‘delete’ ?

If the movie actually gets added, then why should it not be there when the program is run again ?

Yes, if you want. That way you can see the hash after you update it.

This is because when you run code, the memory is temporary, and is only held for the duration of the code. When the code finishes, any updates to variables/hash, etc gets ‘wiped’, and you go back to only having the source code. (A way around this would be to write to a file, but I’m not sure if that’s in the Ruby lessons, and I couldn’t tell you how to do it Ruby, although a quick ‘writing to file’ Google search should suffice if you’re interested).
I hope this helps!

My guess is that we need a real database ?

Yes, so to get around the issue of writing to a file, you can put the whole code (hash adding, updating, etc) inside a while loop (or until) which loops through the code until the user wants to exit.

All the ‘looping’ does is just extend the session beyond just 1 operation. when the program is closed the changes will be lost just the same ?

Yes they will, but at least you will be able to get multiple operations out of it, and see the additions you have made. If you were keen, you could learn how to read/write to a file, and just have it locally stored in the same folder as the code, using something like https://repl.it/.

Think for now just printing the hash after 1 operation is good to make sure the code actually works. maybe look into storing changes later. thanks

1 Like

Use repl to store the changes ?

Oh. I meant repl is a site which allows you to use many different languages and have multiple files with in the same repository. It just makes it easy to use reading/writing to files if you don’t have a Ruby compiler on your computer.

I have Ruby on my Linux, isn’t that an interpreter ?

Oh ok, then you can write Ruby on that and store files in the same place (i.e. on your desktop), in which case you wouldn’t need repl. I also suggested it because its environment is similar to that of CC.

Just to be clear: Ruby is interpreted not compiled ?

Ruby is compiled into bytecode instructions, which are then interpreted into machine code. You can read more about it here: https://buildingvts.com/the-structure-and-interpretation-of-ruby-programs-362db0412f29.

So, Ruby on my Linux is a compiler ? I don’t see where it makes an executable in my project folder.

I am not too familiar with the structure of Ruby compiling and Linux. If you are talking about something similar to Python’s IDLE, then you should be able to write code on that, and it automatically turns it into an executable file. You can then have text files that it reads/writes to. Alternatively, if you wanted to do it on your computer, but not on repl, you could use a text editor (like TextEdit or Notepad; I;m not sure what Linux uses), to write you code, then save it with a .rb (if I remember the Ruby extension), and run it like that.
Sorry I couldn’t be more help!