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 3936592
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:57:59+00:00 2026-05-19T23:57:59+00:00

I have a class that I want to put factory methods on to spit

  • 0

I have a class that I want to put factory methods on to spit out a new instance based on one of two construction methods: either it can be constructed from data in memory, or data stored in a file.

What I would like to do is to encapsulate the logic of how the construction is performed inside the class, so I would like to have static class methods that are set up like this:

class MyAppModel
    def initialize
        #Absolutely nothing here - instances are not constructed externally with MyAppModel.new
    end

    def self.construct_from_some_other_object otherObject
        inst = MyAppModel.new
        inst.instance_variable_set("@some_non_published_var", otherObject.foo)
        return inst
    end

    def self.construct_from_file file
        inst = MyAppModel.new
        inst.instance_variable_set("@some_non_published_var", get_it_from_file(file))
        return inst
    end
end

Is there no way to set @some_private_var on an instance of a class from the class itself without resorting to metaprogramming (instance_variable_set)? It seems like this pattern is not so esoteric as to require meta-poking variables into instances. I really don’t intend to allow any class outside of MyAppModel to have access to some_published_var, so I don’t want to use e.g. attr_accessor – it just feels like I’m missing something…

  • 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-19T23:58:00+00:00Added an answer on May 19, 2026 at 11:58 pm

    Maybe using a constructor is the better way to achieve what you want, just make it protected if you don’t want to create instances from the “outside”

    class MyAppModel
      class << self
        # ensure that your constructor can't be called from the outside
        protected :new
    
        def construct_from_some_other_object(other_object)
          new(other_object.foo)
        end
    
        def construct_from_file(file)
          new(get_it_from_file(file))
        end
      end
    
      def initialize(my_var)
        @my_var = my_var
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Generic Base Class that I want to allow one of two
I have a brain class that I want to get Rest services and put
I have a class that I want to contain multiple objects of something I
I have a class that I want to include in a GWT module. Unfortunately,
I have a templated class that I want to avoid copying (because of the
I have a class PlaylistTrack that I want to extend class Song. When I
I have a helper class that I want to display a UIAlertView then will
Suppose I have a Python class that I want to add an extra property
I have a signed class library that I want to create assemblies for via
I have a class that inherits QMainWindow and I just want it to have

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.