Curious about when to put .to_i and .intern


#1



This is the right code, but I want to know if instead of movies[title.intern] = rating.to_i,
I could write it like this: title = gets.chomp.intern
and rating = gets.chomp.to_i

Is there a difference? It still runs, but it wouldn't let me pass.


case choice
when "add"
  puts "What movie do you want to add?"
  title = gets.chomp.downcase
  puts "On a scale of 1-5, how would you rate #{title}"
  rating = gets.chomp
  movies[title.intern] = rating.to_i
  movies["#{title}"] = rating


#2

You mention two things but seem to be talking about four

title
title.intern
rating
rating.to_i

They're all different, why would they not be? A start would be to consider what title and rating are, and then look up what those methods do

https://ruby-doc.org/core-2.2.0/String.html#method-i-to_i
https://ruby-doc.org/core-2.2.0/String.html#method-i-intern


#3

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