Using ! for to_sym and to_i


#1



https://www.codecademy.com/en/courses/ruby-beginner-en-0i8v1/0/5?curriculum_id=5059f8619189a5000201fbcb


title.to_sym! and rating.to_i! give an error:
undefined method `to_sym!' for "Fight club":String


case choice
when "add" 
    puts "Movie title: "
    title = gets.chomp
    puts "Rating: "
    rating = gets.chomp
    title.to_sym!
    rating.to_i!
    movies[title] = rating
    puts "Added #{title} with rating of #{rating}"
when "update"
    puts "Updated!"
when "display"
    puts "Movies!"
when "delete"
    puts "Deleted!"
else
    puts "Error!"
end


#2

Not every Ruby method has a ! equivalent. The to_i and to_sym methods in Ruby don't have a ! version.

You can see all of the methods available on String in Ruby here.


#3

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