2012-02-23 96 views
0

所以,我知道有一个简单的错误,但我似乎无法找到它。我第一次使用Modules/Mixins,任何帮助将非常感谢。我不断收到此错误:Ruby Mixin未定义方法

undefined method `this_is' for Value:Module (NoMethodError) 

但它看起来像的方法是存在的......这就是我的模块和类...

module Value 
    def this_is 
    puts "#{self.players_hand} is the players hand" 
    end 
end 

require './value.rb' 

class Player 
    include Value 
    attr_accessor :players_hand 

    def initialize 
    @players_hand = 0 
    end 

    def value_is 
    Value.this_is 
    end 
end 

require './player.rb' 

class Game 

    def initialize 
    @player = Player.new 
    end 

    def start 
    puts @player.players_hand 
    puts @player.value_is 
    end 
end 

game = Game.new 
game.start 

回答

1

当你include ValuePlayer类里面,您正在使Value模块的方法成为Player类的一部分,因此this_is方法未命名空间。知道了,我们需要改变这个方法:

def value_is 
    Value.this_is 
end 

要:

def value_is 
    this_is 
end 
+0

很好的解释!谢谢@ctcherry – Alekx 2012-02-23 04:38:57