Ruby 方法\u丢失,在应该运行时未运行
我的程序中有一个团队课程,我正在尝试使用方法 但是,它没有在方法不存在时运行函数,而是给了我一个错误:Team:Class NoMethodError的未定义方法'hawks' 我的代码如下:Ruby 方法\u丢失,在应该运行时未运行,ruby,method-missing,Ruby,Method Missing,我的程序中有一个团队课程,我正在尝试使用方法 但是,它没有在方法不存在时运行函数,而是给了我一个错误:Team:Class NoMethodError的未定义方法'hawks' 我的代码如下: class Team attr_accessor :cust_roster, :cust_total_per, :cust_name, :cust_best_player @@teams = [] def initialize(stats = {}) @cust_roster = st
class Team
attr_accessor :cust_roster, :cust_total_per, :cust_name, :cust_best_player
@@teams = []
def initialize(stats = {})
@cust_roster = stats.fetch(:roster) || []
@cust_total_per = stats.fetch(:per)
@cust_name = stats.fetch(:name)
@cust_best_player = stats.fetch(:best)
@@teams << self
end
def method_missing(methId)
str = methID.id2name
Team.new(roster:[], per: 0, name: str.uppercase, best: 0)
end
class <<self
def all_teams
@@teams
end
end
end
hawks = Team.hawks
您的代码有许多问题。让我们一个接一个地看 根据文件 方法_缺少*args private 当obj收到无法处理的消息时由Ruby调用 这里消息指的是方法。在ruby中,无论何时调用对象上的方法,实际上都是向对象发送消息 为了更好地理解这一点,请在irb shell中尝试这个
1+2
=> 3
1.send(:+,2)
=> 3
这里1和2是Fixnum类的对象。您可以使用1.class来确认这一点。好的,回到你的问题上来。因此,应该对实例调用一个缺少方法的方法
team = Team.new
team.hawks
如果您尝试上面的代码,您将得到一个错误,提示“fetch”:找不到键::花名册键错误
您可以通过将默认值作为第二个参数传递给fetch方法来解决这个问题。将初始化方法替换为
def initialize(stats = {})
@cust_roster = stats.fetch(:roster, [])
@cust_total_per = stats.fetch(:per, 0)
@cust_name = stats.fetch(:name, "anon")
@cust_best_player = stats.fetch(:best, "anon")
@@teams << self
在方法定义中,您接收到一个名为methId的参数,但在该参数中您试图调用methId。把它修好
str = methId.id2name
如果您执行脚本,您将再次得到一个错误,称为undefined method uppercase for hawks:String NoMethodError
这是因为字符串上没有大写方法。您应该改为使用upcase方法
你应该可以走了
有关更多信息,请参阅
希望这有帮助 您的代码存在许多问题。让我们一个接一个地看
class Team
attr_accessor :cust_roster, :cust_total_per, :cust_name, :cust_best_player
@@teams = []
def initialize(stats = {roster: [], per: 0, name: "", best: 0}) # I added the default values here.
@cust_roster = stats.fetch(:roster)
@cust_total_per = stats.fetch(:per)
@cust_name = stats.fetch(:name)
@cust_best_player = stats.fetch(:best)
@@teams << self
end
def method_missing(name, *args)
self.cust_name = name.to_s.upcase
end
class << self
def all_teams
@@teams
end
end
end
team_hawks = Team.new #=> create an instance of Team class, I renamed the object to avoid confusions.
team_hawks.hawks #=> called method_missing which assigned the cust_name variable to "HAWKS"
team_hawks.cust_name #=> HAWKS, so cust_name is assigned to be hawks. This is to check if the assignment worked.
根据文件
方法_缺少*args private
当obj收到无法处理的消息时由Ruby调用
这里消息指的是方法。在ruby中,无论何时调用对象上的方法,实际上都是向对象发送消息
为了更好地理解这一点,请在irb shell中尝试这个
1+2
=> 3
1.send(:+,2)
=> 3
这里1和2是Fixnum类的对象。您可以使用1.class来确认这一点。好的,回到你的问题上来。因此,应该对实例调用一个缺少方法的方法
team = Team.new
team.hawks
如果您尝试上面的代码,您将得到一个错误,提示“fetch”:找不到键::花名册键错误
您可以通过将默认值作为第二个参数传递给fetch方法来解决这个问题。将初始化方法替换为
def initialize(stats = {})
@cust_roster = stats.fetch(:roster, [])
@cust_total_per = stats.fetch(:per, 0)
@cust_name = stats.fetch(:name, "anon")
@cust_best_player = stats.fetch(:best, "anon")
@@teams << self
在方法定义中,您接收到一个名为methId的参数,但在该参数中您试图调用methId。把它修好
str = methId.id2name
如果您执行脚本,您将再次得到一个错误,称为undefined method uppercase for hawks:String NoMethodError
这是因为字符串上没有大写方法。您应该改为使用upcase方法
你应该可以走了
有关更多信息,请参阅
希望这有帮助
class Team
attr_accessor :cust_roster, :cust_total_per, :cust_name, :cust_best_player
@@teams = []
def initialize(stats = {roster: [], per: 0, name: "", best: 0}) # I added the default values here.
@cust_roster = stats.fetch(:roster)
@cust_total_per = stats.fetch(:per)
@cust_name = stats.fetch(:name)
@cust_best_player = stats.fetch(:best)
@@teams << self
end
def method_missing(name, *args)
self.cust_name = name.to_s.upcase
end
class << self
def all_teams
@@teams
end
end
end
team_hawks = Team.new #=> create an instance of Team class, I renamed the object to avoid confusions.
team_hawks.hawks #=> called method_missing which assigned the cust_name variable to "HAWKS"
team_hawks.cust_name #=> HAWKS, so cust_name is assigned to be hawks. This is to check if the assignment worked.
希望这就是你要找的
希望这就是你要找的 你不是说老鹰队=新老鹰队吗?Team.hawks试图调用不存在的类方法hawks。或者def self.method_缺失?不,这不起作用,因为Team.new已经是一个函数,所以它不会运行该方法_missing@ScottJ堆栈级别太深SystemStackError尝试执行hawks=Team.new。那你的意思不是说老鹰队吗?Team.hawks试图调用不存在的类方法hawks。或者def self.method_缺失?不,这不起作用,因为Team.new已经是一个函数,所以它不会运行该方法_missing@ScottJ堆栈级别太深SystemStackError尝试执行hawks=Team.new。那就做老鹰=老鹰