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

  • Home
  • SEARCH
  • 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 553143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:34:27+00:00 2026-05-13T11:34:27+00:00

Currently I have code like the following (simplified somewhat). Eventually, I’ve added more and

  • 0

Currently I have code like the following (simplified somewhat). Eventually, I’ve added more and more new classes like D1/D2, and I think it’s time to do some refactoring to make it more elegant. The goal of course is to make adding new class Dx use as little duplicate code as possible. At least, the duplicate parts of calling FileImporter.import inside the singleton method Dx.import should be factored out.

module FileImporter
  def self.import(main_window, viewers)
    ...
    importer = yield file  # delegate to D1/D2 for preparing the importer object
    ...
  end
end

class D1
  def self.import(main_window)
    viewers = [:V11, ]  # D1 specific viewers
    FileImporter.import(main_window, viewers) do |file|
      importer = self.new(file)
      ...  # D1 specific handling of importer
      return importer
    end
  end
end

class D2
  def self.import(main_window)
    viewers = [:V21,:v22, ]  # D2 specific viewers
    FileImporter.import(main_window, viewers) do |file|
      importer = self.new(file)
      ...  # D2 specific handling of importer
      return importer
    end
  end
end

# client code calls D1.import(...) or D2.import(...)

Essentially FileImporter.import is the common part, with Dx.import being the variation. I’m not sure how to refactor these singleton methods. What is the common Ruby way of doing this?

Update: (some comments were added into code above, hopefully make my intension clearer …)

Originally, I’ve left out code I thought not significant to avoid confusion. I should have mentioned that the code above was also the result of refactoring class D1 and D2 (by moving common part out and into module FileImporter). The purpose of D1.import and D2.import was mainly to create objects of proper class (and possibly followed by some class-specific handling before returning from the block). FileImporter.import is mainly the common logic, within which at some point would yield to specific class for generating the importer object.

I feel that class D1 and D2 and looks really similar and it should be possible to further refactor them. For example, they both call FileImporter.import to supply a block, within which both create an object of itself.

Solution: Originally I didn’t realize you could call base class’s singleton methods simply by calling super from within derived class’s corresponding singleton methods. That was really the main problem I had and was not able to go with that route. So I’ve accepted @makevoid answer as it indeed makes creating new derived classes more easily.

Using a common base class is an elegant refactoring solution, but one problem with that is all new derived classes would already use up the one base class quota. I’ve came to this class macro method which provides even more concise results from the derived class perspective.

module FileImporter
  def self.included(mod)
    mod.extend ClassMethods
  end

  module ClassMethods
    def importer_viewer(*viewers, &blk)
      @viewers = viewers
      @blk = blk

      class << self
        def import(main_window)
          if @blk.nil?
            FileImporter.import(main_window, @viewers) do |file|
              self.new(file)
            end
          else
            FileImporter.import(main_window, @viewers, &@blk)
          end      
        end
      end
    end
  end

  def self.import(main_window, viewers, multi=true)
    ...
    importer = yield file  # delegate to D1/D2 for preparing the importer object
    ...
  end
end

class D1
  include FileImporter
  importer_viewer [:V11, ] do
    ...  # D1 specific handling of importer
  end
end

class D2
  include FileImporter
  importer_viewer [:V21,:v22, ] do
    ...  # D2 specific handling of importer
  end
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-05-13T11:34:28+00:00Added an answer on May 13, 2026 at 11:34 am

    Maybe it’s not the best solution but at first seems that Dx classes share the same behaviour, so sublassing them with a C class that has a self.import method that uses a block to accept some other code could work. Or that could be done by including a module too.

    Anyway, something like this should work (sorry for the shorter names but were good for prototyping).
    Also notice I changed FileImporter.import method name to another to avoid misunderstandings
    and note that i haven’t tested the code 🙂

    module F
      def self.fimport(something)
        yield "file"
      end
    end
    
    
    class C   
      include F 
    
      def initialize(f)
    
      end
    
      def self.import(something, &block)
        F.fimport(something) { |f|
          d = self.new(f)
          block.call
          d
        }
      end
    end
    
    
    class D1 < C
      def self.import(something)
        super(something){
          puts something
        }
      end
    end
    
    
    class D2 < C
      def self.import(something)
        super(something){
          puts something
        }
      end
    end
    
    p D1.import("a")
    p D2.import("b")
    
    #=> a
    #=> #<D1:0x100163068>
    #=> b
    #=> #<D2:0x100162e88>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 324k
  • Answers 324k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The image site specifically says: No color-space metadata and no… May 14, 2026 at 1:10 am
  • Editorial Team
    Editorial Team added an answer What about the DATEDIFF function ? Quoting the manual's page… May 14, 2026 at 1:10 am
  • Editorial Team
    Editorial Team added an answer You could try the Werkzeug modules, the base Werkzeug library… May 14, 2026 at 1:10 am

Related Questions

I have a piece of Perl code somewhat like the following (strongly simplified): There
I am using the Ado.Net Entity Framework with ASP.NET MVC. In my MSSQL 2008
How in JavaScript process unhandled yet part of XMLHttpRequest responseText only in onprogress/onreadystatechange handler,
I want to databind an ItemsCollection, but instead of rendering the collection items, I
I have implemented what I thought was a pretty decent representation of MVC in

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.