ruby
  • hash
  • symbols
  • 2014-02-19 4 views -1 likes 
    -1

    나는 이것을 멀리 만들었지 만, 잘못된 위치에 넣었다고 가정하기 때문에 .to_i와 .to_sym 메서드를 추가 할 위치를 모른다. 누군가 나를 도울 수 있습니까? "NilClass 정의되지 않은 메서드`to_sym = '전무는"Ruby에서 문자열을 기호와 정수로 변환하기

    movies = { 
        spiderman: 3, 
        superman: 4, 
        batman: 5 
    } 
    
    puts "what movie do you like?" 
    choice = gets.chomp 
    
    case choice 
    when 'add' 
    puts "What movie do you want to add?" 
    title.to_sym = gets.chomp 
    puts "what is the rating of that movie?" 
    rating.to_i = gets.chomp 
    movies[title]=rating 
    puts "Added!" 
    
    when 'update' 
        puts "What movie would you like to update?" 
        title = gets.chomp 
        puts "Updated!" 
    when 'display' 
        movies.each do |movies, ratings| 
        puts "#{movies}: #{ratings}" 
        end 
    
        puts "Movies!" 
    when 'delete' 
        puts "What movie would you like to delete from the list?" 
        title = gets.chomp 
        puts "Deleted!" 
    
    else 
        puts "Error!" 
    end 
    

    답변

    1

    []= 외에, 일반 루비에, 형태 foo=, 또는 코드에서와 같은 to_sym=, 할 수있는 방법이 없습니다 내가 밖으로 코드를 테스트 할 때를 말한다 . 당신이 상징으로 사용자의 입력을 얻고 싶은 경우에, 당신은 할 수 있습니다 : 당신이 정수를 얻고 싶다면

    title = gets.chomp.to_sym 
    

    , 당신은 할 수 있습니다 :

    rating = gets.to_i 
    

    난 당신이 한 이유를 모르겠어요 후자의 경우는 chomp입니다.

    +0

    확인. 그게 효과가 있었어. 이제 이해가된다. 방금 도착하자마자 chomp를 추가하는 데 익숙해졌습니다. 그래도 고마워 – user3324987

    관련 문제