2017-02-24 1 views
0

왜 인스턴스 변수에 액세스 할 수 없습니까?rspec --innit; .bash_profile이 작동하지 않습니다.

let(:hotel2) { Hotel.new name: 'Premier Inn', rating: 1, 
       city: 'Leeds', total_rooms: 15, features: [] } 

초기화 할 때 호출하고 있지만 잘못된 인수 오류가 계속 발생합니다.

def initialize() 
    @name = name 
    @rating = rating 
    @city = city 
    @total_rooms = total_rooms 
    @features = features 
    end 

아이디어가 있으십니까?

답변

0

초기화 서명이 사용자의 호출 서명과 일치하지 않습니다. 해시를 전달하고 있지만 수신하지 못했습니다. 이 작업을 수행하기 위해 인수 목록을 정의하는 데는 여러 가지 방법이 있습니다. 여기에 하나입니다

class Hotel 
    def initialize(hash) 
    @name = hash[:name] 
    @rating = hash[:rating] 
    @city = hash[:city] 
    @total_rooms = hash[:total_rooms] 
    @features = hash[:features] 
    end 
end 

This blog post

루비 V2 키워드 인수를 사용하는 방법을 설명합니다. 이는 아마도 initialization을 정의하는 또 다른 방법 일 것입니다. 여기에 그 예가 있습니다 :

class Hotel 
    def initialize(name: , rating:, city:, total_rooms:, features:) 
    @name = name 
    @rating = rating 
    @city = city 
    @total_rooms = total_rooms 
    @features = features 
    end 
end 

키워드 인수에 기본값을 설정하고 필수로 설정할 수 있습니다. 이 예에서는 모두 필수입니다.

관련 문제