Adding .to_sym when asking for input or when adding to hash?


#1



Code works but I added .to_sym and .to_i to the gets.chomp call. Noticed in the example code it's added to movies[title.to_sym] when adding the movie to the hash. I'm wondering if there's an advantage of doing it one way over the other? Does it tax the system less to only convert when adding to the hash? Would make sense since it could jump to the else statement without doing any conversions.


when "add"
  puts "What movie do you want to add?"
  title = gets.chomp.to_sym
  if
      movies[title.to_sym].nil?
      puts "What rating do you want to give it? (from 1 - 4)"
      rating = gets.chomp.to_i
      movies[title] = rating
      puts "Your movie was added!"
  else
      puts "This movie already exists in the database!"
  end


#2

Since you're using the input immediately after reading it, those descriptions both describe the same moment in time, there is no difference


#3

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