6/2 - Setting up - Comma or no comma?


#1

I got to the end and everything works but I seem to need to add a final comma to my hash when i create it at the beginning.

In the movies hash, when I add a comma after the Ishtar rating, everything works fine; but when I remove it, the program seems to run right through to the else in the case and prints the error.

I thought you weren’t supposed to include that last comma in the hash. The initial example doesn’t have this comma. I even copied it exactly from the example and it still didn’t work. Am I wrong?

movies = {
  Memento: 3,
  Primer: 4,
  Ishtar: 1,  # <= Only works if I have this comma
}

puts "To add a movie rating, type 'add'."
puts "To update a movie rating, type 'update'."
puts "To display all movies in the list, type 'display'."
puts "To delete a movie from the list, type 'delete'"

puts "What would you like to do?"
choice = gets.chomp


case choice
  when "add"
  	puts "What movie would you like to add?"
  	title = gets.chomp
  	if movies[title.to_sym].nil?
  		puts "What rating would you like to give it?"
  		rating = gets.chomp
  		movies[title.to_sym] = rating.to_i
  		puts "Movie has been added!"
    else
      puts "That movie already exists!"
    end
  when "update"
  	puts "What movie would you like to add?"
  	title = gets.chomp
  		if movies[title.to_sym].nil? == false
      	puts "This movie is not in the list."
    	else
        puts "What rating would you like to give it?"
        rating = gets.chomp
      	movies[title.to_sym] = rating.to_i
      end
  when "display"
  	movies.each do |movie, rating|
      puts "#{movie}: #{rating}"
    end
  when "delete"
  	puts "Which title would you like to delete?"
  	title = gets.chomp
      if movies[title.to_sym].nil?
        puts "That movie is not in the list"
    else
      movies.delete(title.to_sym)
        puts "Title had been deleted!"
    end
  else
  	puts "Error! Did you type in the right command?"
end

#2

The comma would not be needed, and your code seems to work without it, as expected. What is it that is not working, exactly?

Nice Work!

Aside

By this stage in project our hash should be written as symbols:

:Ishtar => 1

#3

That’s strange, it wasn’t working when i took out that comma before but now it does… At least I know there shouldn’t be a comma moving forward. Thanks!

Isnt Ishtar: 1 the newer syntax of writing symbols as hashes? As opposed to the old “hash rocket” syntax?

From 6/12-The hash rocket has landed:

However, the hash syntax changed in Ruby 1.9. Just when you were getting comfortable!

The good news is that the changed syntax is easier to type than the old hash rocket syntax, and if you’re used to JavaScript objects or Python dictionaries, it will look very familiar:

new_hash = { 
  one: 1,
  two: 2,
  three: 3
}

puts new_hash
# => { :one => 1, :two => 2, :three => 3 }

From now on, we’ll use the 1.9 hash syntax when giving examples or providing default code. You’ll want to be familiar with the hash rocket style when reading other people’s code, which might be older.


#4

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