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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:09:13+00:00 2026-05-29T19:09:13+00:00

I have a class whose new method has been made private because I want

  • 0

I have a class whose new method has been made private because I want only my class method “constructors” to create new instances. However, I also now need to have some instance methods create new instances too.

For example, look at the following snippet. It is in method_a where I’m having trouble:

Class Foo

  class << self

    #a "constructor" method
    def from_x arg
      stuff_from_arg = arg.something #something meaningful from arg
      new(stuff_from_arg)
    end
  end

  def initialize stuff
    @stuff = stuff
  end

  private_class_method :new #forces use of Foo.from_x

  def method_a
    other_stuff = "blah"
    #These do not work
    return new(blah) #nope, instance cannot see class method
    return self.class.new(blah) #nope, new is private_class_method
    return Foo.new(blah) #same as previous line
    return initialize(blah) #See *, but still doesn't work as expected
  end
end

What I’m starting to think is that I may have designed this class in such a way that I need to make another class constructor method to create a new Foo, like this:

#in addition to previous code
Class Foo
  class << self
    def just_like_new stuff
      new(stuff)
    end
  end
end

This doesn’t feel very right or very DRY, but perhaps this is the bed I’ve made for myself. Is there anything I can do?

*This line has been a bit surprising. It returns blah. Outside of a class definition, there is also an initialize and it takes 0 arguments and returns nil Does anyone know what is happening here?

  • 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-29T19:09:14+00:00Added an answer on May 29, 2026 at 7:09 pm

    You can always use send to get around the access control:

    def from_x arg
      stuff_from_arg = arg.something #something meaningful from arg
      self.class.send(:new, stuff_from_arg)
    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 whose instances have attributes that are containers which themselves contain
Previously I have created a method updateRecord() in com.TestTransaction class. The updateRecord() method has
i have a class whose equality is based on 2 fields such that if
We have a class whose semantic behaviour is like the following :- struct Sample
I have a class whose constructor takes a const reference to a string. This
I have a class whose functionality I'd like to depend on a set of
I have a CurrentUser class with a static property whose value was stored in
Suppose I have a file X.h which defines a class X, whose methods are
I have a Struts + Velocity structure like for example, a Person class, whose
I have class method that returns a list of employees that I can iterate

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.