2012-12-18 6 views
1

codeschool의 루비 비트 코스에서 나는이 클래스들이 어떻게 작동하는지 이해하려고 노력하고있다. 나는 Game 클래스와 게임 컬렉션을 저장하는 Library이라는 콜렉션 클래스를 가지고있다.왜 내 루비 메서드가 항상 true를 반환합니까?

class Game 
    attr_accessor :name, :year, :system 
    attr_reader :created_at 

    def initialize(name, options={}) 
    self.name = name 
    self.year = options[:year] 
    self.system = options[:system] 
    @created_at = Time.now 
    end 


    def ==(game) 
    name == game.name && 
    system == game.system && 
    year == game.year 
    end 
end 

라이브러리 클래스 :

contra = Game.new('Contra', { 
    year: 1994, 
    system: 'nintendo' 
}) 

mario = Game.new('Mario', { 
    year: 1996, 
    system: 'SNES' 
}) 

sonic = Game.new('Sonic', { 
    year: 1993, 
    system: 'SEGA' 
}) 

및 새 컬렉션의 인스턴스 : 나는 경우 찾을 때

myCollection = Library.new(mario, sonic) 

class Library 
    attr_accessor :games 

    def initialize(*games) 
    self.games = games 
    end 

    def has_game?(*games) 
    for game in self.games 
     return true if game == game 
    end 
    false 
    end 
end 

가 지금은 어떤 게임을 만들 특정 게임은에 있습니다.210 has_game? 메서드를 사용하면 컬렉션의 일부로 삽입 된 적이 없지만 항상 true

puts myCollection.has_game?(contra) #=> returns **true**이 표시됩니다.

내가 뭘 잘못하고 있니?

+3

'때문에 game == game'은 항상 사실입니다. – melpomene

+0

Game 클래스의'== (games)'인스턴스 메소드가 문제입니까? 이 문제를 어떻게 해결할 수 있습니까? 게임이 콜렉션의 일부인지 아닌지 확인해야합니다. –

+2

아니요, 문제는 게임 자체를 게임과 비교한다는 것입니다. – melpomene

답변

1

잘못 여기에 몇 가지가 있습니다 :

당신이 뭔가를 할 수 있습니다

대신 인스턴스 변수를 만들 self.XXXX를 사용
  1. 당신이해야 사용 @XXXX, 그것은 직접 self를 사용하여 값에 직접 액세스합니다. 다른 메소드 호출에 대한 자세한 내용은 여기를 참조하십시오. Instance variable: self vs @

  2. 다른 사람으로
  3. 항상 true를 반환합니다 game == game 언급, 이미 has_game?

다음

제대로 작동 내 변화는 단일 게임보다 더 통과를 허용하지 않는 게시 그 답 :

class Game 
    attr_accessor :name, :year, :system 
    attr_reader :created_at 

    def initialize(name, options={}) 
    @name  = name 
    @year  = options[:year] 
    @system  = options[:system] 
    @created_at = Time.now 
    end 


    def ==(game) 
    @name == game.name && 
    @system == game.system && 
    @year == game.year 
    end 
end 

class Library 
    attr_accessor :games 

    def initialize(*games) 
    @games = games 
    end 

    # only returns true if this Library 
    # has ALL of the games passed to has_game? 
    def has_game?(*_games) 
    _games.each do |game| 
     return false if not @games.include?(game) 
    end 

    return true 
    end 
end 

contra = Game.new('Contra', { 
    year: 1994, 
    system: 'nintendo' 
}) 

mario = Game.new('Mario', { 
    year: 1996, 
    system: 'SNES' 
}) 

sonic = Game.new('Sonic', { 
    year: 1993, 
    system: 'SEGA' 
}) 

myCollection = Library.new(mario, sonic) 
puts "Collection has Contra? #{myCollection.has_game?(contra)}" 
puts "Collection has Sonic and Mario #{myCollection.has_game?(sonic, mario)}" 

출력 :

Collection has Contra? false 
Collection has Sonic and Mario true 
+0

굉장해! 이것은 정확하게 내가 찾고 있었던 것입니다 –

+0

"has_game?()"메서드는 마지막 게임이 @games에 포함되어 있는지 여부를 반환합니다 ... 아마도 배열 교차점에서 더 쉬울 것입니다. 그렇지 않으면 def has_game? – Pavling

+0

@ Pavling 당신 말이 맞아요, 거짓의 경우에 탈옥하는 걸 잊었어요. (게임 _ –

2
return true if game == game 

이 진술은 문제가 발생할 수 있습니다.

항상 사실입니다.

def has_game?(wanted) 
    for game in self.games 
    return true if game == wanted 
    end 
    false 
end 
+0

그럼 어떻게 해결할 수 있습니까? –

관련 문제