0

サブクラスからの一連の一般的なコードをスーパークラスメソッドに分解したいと思います。スーパークラスメソッドは、サブクラスで定義される存在しない(スーパークラス内の)メソッドを参照する必要があります。しかし、これを機能させることはできません。

これは、私が試した多くの複数のバリエーションのうちの1つの試みです。

class Superclass
    def chunk_of_code
        # <code...>
        nonexistant_superclass_method_defined_in_subclass params
        # <more code...>
    end
end

class Subclass < Superclass
    def nonexistant_superclass_method_defined_in_subclass params
        # whatever...
    end
end

Subclass.new.chunk_of_code params

これは機能しません。他のバリエーションも機能しません。この種のコーディングはRubyで可能ですか(私はそう思っていました)?私はSmalltalkでいつもこのようなことをしていました。

私が望むことを達成する方法はありますか?今はRubyの継承を学び、使用したいので、「ミックスイン」や「モジュール」の使用を勧めないでください。

*最新バージョンのRubyを実行しています。

ありがとう。

編集:これはRailsアプリにあります。スーパークラスはApplicationControllerです。

編集:これは私がこれをやろうとした多くの反復の1つからの実際のコードです。この特定の例では、ビューに「undefined method `each'for nil:NilClass」が含まれています。これは、すべてがサブではなくスーパー(定義されていない場合)のコンテキストで実行されているため、または少なくともそれが私の解釈です:

class ApplicationController < ActionController::Base
    protect_from_forgery
    before_filter :authenticate_registration!

    # models and x defined in subclass
    def index
        models = x.where registration_id: current_registration.id

        respond_to do |format|
            format.html # index.html.erb
            format.json { render json: models }
        end
    end
    # more code here...
    # ...
end

class PositionsController < ApplicationController
    def x
        Position
    end

    def models= blah
        @positions = blah
    end

    # more code here...
    # ...
end
4

4 に答える 4

3

ここで発生する唯一のエラーは、の定義ですchunk_of_code。このメソッドは、次のようないくつかの仮パラメータを受け入れる必要があります。

def chunk_of_code params

そして、あなたはそれを自由に呼ぶことができます:

params = 'something'
Subclass.new.chunk_of_code params
于 2012-06-04T13:32:07.833 に答える
1

あなたのエラーは実際には継承とは何の関係もなく、この行にあります

models = x.where registration_id: current_registration.id

これは潜在的にあいまいです。これは、メソッドを呼び出すことを意味しますか、それともmodels=というローカル変数に割り当てることを意味しますmodelsか?この(および同様の)状況では、rubyはローカル変数を処理しようとしていると想定しています。代わりにメソッドを呼び出したい場合は、次のことを行う必要があります。

self.models = x.where registration_id: current_registration.id

models=メソッドが呼び出されないので、nil@positionsであり、ビューがそれを使用しようとしていると思います。

この一般的なコントローラーのものを処理するmake_resourcefulなどのgemにも興味があるかもしれません。

于 2012-06-04T19:18:53.067 に答える
0

Railsのモデル側では、私は日常的に次のものを使用します。

class GenericModel < ActiveRecord::Base
  self.abstract_class = true

  # define all the generic behavior methods/ model stubs you want.
  # model-specific classes can override to their hearts content
  # or use the inherited implementation

end

class Feature < GenericModel
  # model specific methods, and/or overrides
end

そして私は

class GenericController
  # basic show implementation for example
  def show
    @object = params[:controller].singularize.camelcase.constantize.find(params[:id])
    respond_to do |format|
      format.pdf { render :layout => false }
      format.html  # show
      format.xml { render :xml => @object.to_xml }
    end
  end
end

特定のモデルのshowbehaviorがgenericと変わらない場合、そのメソッドはその'model'_controller.rbに表示されません。

于 2012-06-04T19:48:13.163 に答える
-1

raise NotImplementedErrorこれを行う方法は、親でメソッドの動作として定義することです。ちなみに、あなたがやろうとしているのは抽象クラスを作成することです。これは、Javaのような他の特定の言語でより容易になります。

于 2012-06-04T13:27:51.647 に答える