7. Update


#1

Hello everyone!

When I tried to check my code I receive this error message:

Oops, try again. Make sure to test your program by choosing 'update'."

Somehow it's passed "add" function, but now it's doesn't. And yep, I'm using update option correctly. xD
It's because I'm transforming choice directly in sym, or something else here?

movies = { Exam: [2009, 3.7], Interstate_60: [2001, 4.0] }

puts "Good time.\tChoose one option."
puts "\t\tadd\tdelete\tupdate"
puts "\t\thelp\tdisplay\tquit"

choice = gets.chomp.downcase.intern

case choice
when :add
    puts "Enter the name of the movie, please."
    title = gets.chomp.gsub(/ /, "_")
    title.capitalize!
    if movies[title.to_sym].nil?
        puts "When it's been maded (year only)?"
        year = gets.chomp
        puts "Add a rating between \"0.0\" and \"4.0\""
        rating = gets.chomp
        data = [year.to_i, rating.to_f]
        movies[title.to_sym] = data
    else
        print "This movie allready in the list! "
        puts "Created in: #{movies[title.to_sym][0]}. Rating is [#{movies[title.to_sym][1]}]."
    end
when :update
    puts "Enter the name of the movie, please."
    title = gets.chomp.gsub(/ /, "_")
    title.capitalize!
    if movies[title.to_sym].nil?
        puts "No such movie in the list. Check your input please."
    else
        puts "Add a rating between \"0.0\" and \"4.0\""
        rating = gets.chomp
        movies[title.to_sym][1] = rating.to_i
        # puts "#{title.gsub(/_/, " ")} has been updated!"
    end
when :display
    puts "Movies!"
when :delete
    puts "Deleted!"
when :help
    puts "HELP!"
when :quit
    exit
else
    puts "Error!"
end

P.S.: I'm not a native speaker, so I'm terribly sorry for my english.
P.P.S.: Many thanks for any hint. :3


#2

It looks like you are attempting to improve the code before writing what the lesson checker is expecting. Suggest complete the program according to instructions, first, then after completing the module go to town with improvements. Pardom my presumption if indeed wrong.

Now assuming you are finished the module and working at improvements. we can see repetition which suggests a helper function or two would be in order. Take for instance this line,

choice = gets.chomp.downcase.intern

As you know it creates a symbol from an input string. We can expand this line to a method that returns a snake_case symbol.

def choose 
    gets.chomp.downcase.gsub(/ /, '_').intern
end
 > choice = choose()
â–‘Motorcycle Diaries
=> :motorcycle_diaries

This lets us remove title.to_sym from the code when we channel all inputs through the method.

To display the symbol we can create another helper method:

def reveal(sym)
    words = sym.to_s.gsub(/_/, ' ').split
    words.each { |word| word.capitalize! }
    words.join ' '
end
 > reveal(choice)
=> "Motorcycle Diaries"

Looking at the program from the perspective of end game, we can build a basic loop to allow continual use until quit is entered:

loop do
    puts "Good time.\tChoose one option."
    puts "\t\tadd\tdelete\tupdate"
    puts "\t\thelp\tdisplay\tquit"
    choice = choose()
    case choice
    when :add
        puts "Added!"
    when :update
        puts "Updated!"
    when :display
        puts "Movies!"
    when :delete
        puts "Deleted!"
    when :help
        puts "HELP!"
    when :quit
        puts "User aborted"
        break
    else
        puts "Input error"
    end
end

#3

You're absolutely right. :smiley:

Thanks alot for your response. I've already finished this module. But yep, I'm really want to improve my code. (:

So it's not important to use return I guess. Am I wrong?
Never seen such a syntax before. It's because i'm noob in Ruby I think. xD

Is it necessary to use loop do or it's normal to use while true instead?
Don't see any difference between these loops. Is it a bad coding style to use while true?


#4

Methods return the value of the last statement executed.

If the method has no parameters the parentheses can usually be omitted.

https://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Method_Calls

In this situation there is no iteration, only cycling back to the choice prompt, so speed is not a concern. Were we concerned with speed, then while may be more preferrable.


#5

Thanks a lot one more time. (:


#6

Proof of concept…

Code
=begin
https://discuss.codecademy.com/t/7-update/53870
A Night at the Movies
lab study by Roy (@mtf)
=end

def hr
    puts '-' * 40
end

def get_sym 
    gets.chomp.downcase.gsub(/ /, '_').intern
end

def reveal(sym)
    sym.to_s.gsub(/_/, ' ').split.each {|word| word.capitalize!}.join ' '
end

movies = {}

loop do
  hr()
  puts "[add] [delete] [update] [display] [quit]"
  print "Enter a command: "
  command = get_sym()
  hr()
  case command
    when :add
      print "Enter movie title: "
      title = get_sym()
      if movies[title].nil?
        print "Year of release?"
        year = gets.chomp
        print "Add a rating between '0.0' and '4.0'"
        rating = gets.chomp
        data = [year.to_i, rating.to_f]
        movies[title] = data
        puts "#{reveal(title)} added to movies hash."
        puts "Released: #{movies[title][0]}. Rating: #{movies[title][1]}."        
      else
        print "This movie already in the list! "
        puts "Released: #{movies[title][0]}. Rating: #{movies[title][1]}."
      end
    when :update
      print "Enter movie title: "
      title = get_sym()
      if movies[title].nil?
        print "The movie #{reveal(title)} was not found in the movies hash."
      else
        puts "Released: #{movies[title][0]}. Rating: #{movies[title][1]}."
        print "Update year of release: "
        year = gets.chomp
        print "Update rating between '0.0' and '4.0': "
        rating = gets.chomp
        data = [year.to_i, rating.to_f]
        movies[title] = data
        puts "#{reveal(title)} updated in the movies hash."
        puts "Released: #{movies[title][0]}. Rating: #{movies[title][1]}."        
      end
    when :display
      movies.each do |title, data|
        puts "#{reveal(title)}. Released: #{data[0]}. Rating: #{data[1]}."        
      end
    when :delete
      print "Enter movie title: "
      title = get_sym()
      if movies[title].nil?
        puts "The movie #{reveal(title)} was not found in the movies hash."
      else
        movies.delete(title)
        puts "#{reveal(title)} deleted from the movies hash."
      end
    when :quit
        puts "User aborted"
        break
    else
        puts "Input error"
    end
end

#7

def reveal(sym)
    sym.to_s.gsub(/_/, ' ').split.each { |word| word.capitalize! }.join ' '
end

:smiley_cat:


4. Prompting: Redux!
#8

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