Symbol Uniformity


#1



I had a design question. I am trying to make my symbols completely uniform by converting user movie title input into lower case letters and replacing the spaces with '_' so when looking through my hash table there will only be one match for each movie title.
Ex: The user input of: "Star Wars IV" would become "star_wars_iv" before becoming a symbol. However, this is proving to be stubborn.

Any suggestions?
Also a furthering question, when a string is converted into a symbol what would "this is a symbol" be turned into? (because I thought you can't have spaces)


when "update"
    puts "enter a movie title to update: "
    title = gets.chomp.downcase
    title.gsub!(/' '/, '_')  #trying to use this regex to replace a space with _
    puts "#{title}"   #used to de-bug
    if movies[title.intern].nil?
        puts "that movie does not currently exist in the database"
    else
        puts "enter a new rating for this title:"
        rating = gets.chomp
        movies[title.intern] = rating.to_i
        puts "#{title}'s rating has been updated to #{rating}"
    end


#2

There's nothing saying you can't have spaces in symbols, it's just another character
What's stopping you from from doing what you describe? Considering the problem might say a lot about what needs to be done instead. For example, if the problem is that modifying the dictionary while also iterating through it has bad effect on the iteration, then what other ways are there to end up with a dictionary with the keys modified like you want?


#3


Yeah Ruby isn't fond of spaces when declaring them within the code, however if you declare them within the running shell then it does save it with a space.
So I'm hunting down a solution to resolve the issue.


#4

Well, that's not exactly true..

=> "this is a symbol".to_sym
:"this is a symbol"

#5

Which issue?


#6

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