标签: method-missing

在smalltalk中重新定义nil以响应任何消息

我想重新定义smalltalk的nil,就像在objective-c中那样工作.因此,当nil收到无法处理的消息时,它返回nil.现在我知道这nil只是一个快捷方式,UndefinedObject但有没有像Ruby这样的东西method_missing,所以我可以重新定义它以便总是返回nilUndefinedObject

null smalltalk pharo method-missing

1
推荐指数
1
解决办法
1171
查看次数

没有为此对象定义的无参数构造函数.

目前我有一个运行时异常:'Systen.MissingMethodException:没有为此对象定义无参数构造函数.

我用Google搜索并遇到很多人实际上没有默认的contstructor,虽然我的代码确实如此,所以我不知道它出错了!

我的完整代码可以在pastebin上找到,因为它很大:http://pastebin.com/RxdKgxSx

谢谢你的帮助!

asp.net-mvc missingmethodexception method-missing asp.net-mvc-4

1
推荐指数
1
解决办法
2304
查看次数

Method_missing 在它应该运行的时候没有运行

我的程序中有一个 Team 类,我正在尝试使用 method_missing,但是当该方法不存在时,它没有运行该函数,而是给了我一个错误:“Team:Class 的未定义方法 `hawks'(NoMethodError)”

我的代码如下:

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
Run Code Online (Sandbox Code Playgroud)

ruby method-missing

1
推荐指数
1
解决办法
158
查看次数

使用括号和不使用括号调用Ruby中的method_missing之间的区别

是否有可能在Ruby中的method_missing声明中建立是否使用括号表示法调用给定的missing_method(没有任何参数),即:

foo.non_existing_method()
Run Code Online (Sandbox Code Playgroud)

或使用无括号的表示法:

foo.non_existing_method
Run Code Online (Sandbox Code Playgroud)

我需要这个来解决我非常具体的测试问题.

ruby method-missing

0
推荐指数
1
解决办法
256
查看次数

Ruby - ArgumentError:参数个数错误(2个为1)

我有以下类覆盖:

class Numeric
  @@currencies = {:dollar => 1, :yen => 0.013, :euro => 1.292, :rupee => 0.019}
  def method_missing(method_id)
    singular_currency = method_id.to_s.gsub( /s$/, '').to_sym
    if @@currencies.has_key?(singular_currency)
      self * @@currencies[singular_currency]
    else
      super
    end
  end

  def in(destination_currency)
    destination_curreny = destination_currency.to_s.gsub(/s$/, '').to_sym
    if @@currencies.has_key?(destination_currency)
      self / @@currencies[destination_currency]
    else
      super 
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

每当in的参数为复数时,例如:10.dollars.in(:yens)我得到ArgumentError: wrong number of arguments (2 for 1)但不10.dollars.in(:yen)产生错误.知道为什么吗?

ruby metaprogramming numeric method-missing

0
推荐指数
1
解决办法
8726
查看次数

Rails Method_missing从散列中返回值

我正在尝试编写一个method_missing方法,这样当我运行一个方法时,它必须点击哈希并查看键,如果它找到匹配以返回值.并继续.哈希是从我写的sql查询填充的,所以值永远不会是常量.

一个例子就像

 @month_id.number_of_white_envelopes_made
Run Code Online (Sandbox Code Playgroud)

在哈希

 @data_hash[number_of_white_envelopes_made] => 1
Run Code Online (Sandbox Code Playgroud)

所以@month_id将返回1.我以前从未使用过它,没有多少材料使用哈希作为回落方法丢失

编辑:抱歉,我忘了说,如果它没有在哈希中找到方法,那么它可以继续向下没有方法错误

编辑:好吧,所以我被黑客攻击,这就是我想出的

 def method_missing(method)
   if @data_hash.has_key? method.to_sym
     return @data_hash[method]
   else
     super
   end
 end
Run Code Online (Sandbox Code Playgroud)

有没有更好的办法?

ruby ruby-on-rails method-missing

0
推荐指数
1
解决办法
1720
查看次数