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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T13:27:19+00:00 2026-05-29T13:27:19+00:00

MyClass objects have a, b, and c instance variables (forgive my vague abstractions). Additionally,

  • 0

MyClass objects have a, b, and c instance variables (forgive my vague abstractions). Additionally, when writing these classes, I want to be able to set a, b, and c, but I don’t want my users to be able to.

class MyClass
  attr_accessor :a, :b, :c
  private :a=, :b=, :c=
end

I would like MyClass to be instantiated via either of 2 ways such that I would like to be able to call the following:

f = MyClass.create_from_foo foo_data
b = MyClass.create_from_bar bar_data

and get instances of MyClass in f and b. f and b are indistinguishable once instantiated, but of course were created in different ways. After these calls, both f and b have an a, b, and c.

I would not like MyClass to be instantiated through new. Since these 2 ways of creating are both equally valid, I feel like “preferring” foo_data over bar_data or vice versa isn’t an implication I’d like to make. Therefore, I feel like I should privatize new and only allow instances of MyClass to be created by these class methods to try to level the playing field. In other words, I’m trying to avoid this:

#don't want this
f = MyClass.new foo_data
b = MyClass.create_from_bar bar_data #not quite "fair" to bar_data

So, let me start to write these create_from_<type> class methods:

class MyClass

  #from earlier
  attr_accessor :a, :b, :c
  private :a=, :b=, :c=

  private_class_method :new

  def MyClass.create_from_foo foo_data
    #some parsing of foo_data and computation of a, b, and c
    myclass = MyClass.new
    myclass.a = a
    myclass.b = b
    #...
  end

  def MyClass.create_from_bar bar_data
    #again, computation of bar_data
    myclass.a = a
    myclass.b = b
    #...
  end
end

Now, if you’ve been following along, you’d notice I can’t do this! I blocked myself out!

  • I privatized new because I didn’t want my users to make MyClass objects this way. I want them to use the class methods. And in the class method definition space, I’m basically a user.
  • I privatized a=, b=, and c= because I don’t want my users to change this stuff. (It would break my real world example). Again, however, I’m a user when defining a class method.

So, this is what I’m working with. How can I create instances of a “restrictive” class in class methods?

  • 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-29T13:27:20+00:00Added an answer on May 29, 2026 at 1:27 pm

    You were a bit tangled, you can do it a little simplier.

    class MyClass
      attr_reader :a, :b, :c
    
      private_class_method :new
    
      def initialize(a,b)
        @a = a
        @b = b
      end
    
      def self.create_from_foo foo_data
        #some parsing of foo_data and computation of a, b, and c
        myclass = new(a,b)
        #...
      end
    
      def self.create_from_bar bar_data
        #again, computation of bar_data
        myclass = new(a,b)
        #...
      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 class MyClass. It has instance variables passedInVar1, passedInVar2, etc. whose values
I have an object instance called myClass and i need to set the value
I have a set of class instances which have a BOOL instance variable called
I have a c++ class IPoint and want to use an instance of IPoint
I have a Ruby class. I want to get an instance variable from an
I have a class MyClass , which contains two member variables foo and bar
I have a class: class MyClass(object): @property def myproperty(self): return 'hello' Using mox and
I have a __new__ method as follows: class MyClass(object): def __new__(cls, *args): new_args =
I have a list of class items List<MyClass> I have a seperate object that
Is one of these two ways to create and initialize an object preferable? MyClass

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.