Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby-Mixin未定义方法_Ruby_Module_Mixins - Fatal编程技术网

Ruby-Mixin未定义方法

Ruby-Mixin未定义方法,ruby,module,mixins,Ruby,Module,Mixins,所以,我知道有一个简单的错误,但我就是看不出来。我第一次使用Modules/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 './va

所以,我知道有一个简单的错误,但我就是看不出来。我第一次使用Modules/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

Player
类中包含Value
时,将
Value
模块的方法作为
Player
类的一部分,因此
this\u is
方法没有名称空间。知道了这一点,我们需要改变这种方法:

def value_is
  Value.this_is
end
致:


Player
类中包含Value
时,将
Value
模块的方法作为
Player
类的一部分,因此
this\u is
方法没有名称空间。知道了这一点,我们需要改变这种方法:

def value_is
  Value.this_is
end
致:


很好的解释!谢谢,@ctcherry很好的解释!谢谢,@ctcherry