Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8031453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:06:18+00:00 2026-06-05T01:06:18+00:00

I would like to factor a bunch of common code from subclasses into a

  • 0

I would like to factor a bunch of common code from subclasses into a superclass method. The superclass method must refer to a nonexistent (in the superclass) method that will be defined in the subclasses. But I can’t get this to work.

This is one try out of many multiple variations I 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. Is this kind of coding possible in Ruby (I thought it was)? I did this kind of thing all the time working in Smalltalk.

Any way to achieve what I want? Please avoid advising me to use “mix-ins” or “modules,” as I’d just like to just learn and use Ruby’s inheritance for right now.

*Running latest version of Ruby.

Thanks.

EDIT: This is in a Rails app. The superclass is ApplicationController.

EDIT: Here is actual code from one of many iterations I’ve tried to do this. This particular example craps out with “undefined method `each’ for nil:NilClass” in the view, apparently because the whole thing is running in the context of the super (where it isn’t defined) instead of the sub, or at least that’s my interpretation:

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T01:06:20+00:00Added an answer on June 5, 2026 at 1:06 am

    Your error is actually nothing to do with inheritance and is on this line

    models = x.where registration_id: current_registration.id
    

    This is potentially ambiguous: does this mean call the method models= or does it mean assign to a local variable called models? In this (and similar) situation ruby assumes you’re trying to deal with the local variable. If you want to call the method instead you need to do

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

    Since you models= method doesn’t get called, @positions is nil and I assume your view tries to use it.

    You might also be interested in gems such as make_resourceful that handle this common controller stuff.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script with a factory method that I would like to return
I have a C++ code that's a physics simulation tool. I would like to
Would like to make anapplication in Java that will not automatically parse parameters used
Would like to know the c# code to actually retrieve the IP type: Static
I would like to parse the values from aspx pages, both what is passed
I would like to scramble a word with a factor. The bigger the factor
I would like to be able to create multiple combinations that sum to 100%,
I would like to make a subset of a data frame in R that
I have a class with a bunch of properties that look like this: public
I would like to make a series of plots using ggplot from multiple different

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.