Ruby: Mildly 'exotic' inheritance doesn't work? -
i factor bunch of common code subclasses superclass method. superclass method must refer nonexistent (in superclass) method defined in subclasses. can't work.
this 1 try out of many multiple variations have tried:
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 this doesn't work. other variations don't work either. kind of coding possible in ruby (i thought was)? did kind of thing time working in smalltalk.
any way achieve want? please avoid advising me use "mix-ins" or "modules," i'd just learn , use ruby's inheritance right now.
*running latest version of ruby.
thanks.
edit: in rails app. superclass applicationcontroller.
edit: here actual code 1 of many iterations i've tried this. particular example craps out "undefined method `each' nil:nilclass" in view, apparently because whole thing running in context of super (where isn't defined) instead of sub, or @ least that's interpretation:
class applicationcontroller < actioncontroller::base protect_from_forgery before_filter :authenticate_registration! # models , x defined in subclass def index models = x.where registration_id: current_registration.id respond_to |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
your error nothing inheritance , on line
models = x.where registration_id: current_registration.id this potentially ambiguous: mean call method models= or mean assign local variable called models? in (and similar) situation ruby assumes you're trying deal local variable. if want call method instead need do
self.models = x.where registration_id: current_registration.id since models= method doesn't called, @positions nil , assume view tries use it.
you might interested in gems such make_resourceful handle common controller stuff.
Comments
Post a Comment