所以,我知道有一个简单的错误,但我似乎无法发现。我是第一次使用Modules/Mixins,如果有任何帮助,我们将不胜感激。我一直收到这个错误:
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
当您在Player
类中include Value
时,您将使Value
模块的方法成为Player
类的一部分,因此this_is
方法没有名称空间。知道了这一点,我们需要改变这种方法:
def value_is
Value.this_is
end
收件人:
def value_is
this_is
end