0

Railsの開発は初めてです。メソッドのエイリアスをいくつか作成しましたが、どのエイリアスが呼び出されているかを知りたいです。

私はこのコードを持っています。

alias_method :net_stock_quantity_equals :net_stock_quantity
alias_method :net_stock_quantity_gte :net_stock_quantity
alias_method :net_stock_quantity_lte :net_stock_quantity
alias_method :net_stock_quantity_gt :net_stock_quantity
alias_method :net_stock_quantity_lt :net_stock_quantity

def net_stock_quantity
  #some code here
end

ユーザーがどのエイリアスを呼び出したか知りたいです。ユーザーが電話をかけた場合のように、ユーザーが電話をかけなかっnet_stock_quantity_equalsたことを知っておく必要があります。net_stock_quantity_equalsnet_stock_quantity

どんな助けでもいただければ幸いです。

4

3 に答える 3

2

method_missingをオーバーライドしてそれを行うことができます。

def method_missing(method_name, *args, &block)
  if method_name.to_s =~ /^net_stock_quantity_/ 
    net_stock_quantity method_name
  else
    super
  end
end

def net_stock_quantity(alias_used = :net_stock_quantity)
  #some code
end

http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-missing-methods/に似た何かを行うチュートリアルがここにあります

于 2012-09-07T01:27:10.793 に答える
1

問題に誤ってアプローチしていると思われます-エイリアスメソッドを使用する代わりに:equals, :gte, :lte、メソッドへのパラメータとして送信などを行います。

def net_stock_quantity(type = :all)
  # do something with the type here
end 
于 2012-09-05T15:45:38.650 に答える
0
def net_stock_quantity(alias_used = :net_stock_quantity)
    method_called = caller[0]
    #some code
end

method_calledには、呼び出されたエイリアスの名前が含まれます。

于 2012-09-07T09:36:56.293 に答える