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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:28:04+00:00 2026-05-31T12:28:04+00:00

I am getting confused as to how to properly set variables in a initializer,

  • 0

I am getting confused as to how to properly set variables in a initializer, I want these to be class level variables, not instance.

And I also want to then create a single instance of another object (it is a connection object, which already has connection pooling built in, so I just need a single reference to it).

My initializer /initializers/my_class.rb

yml = YAML.load_file("#{Rails.root}/config/my_class.yml")

MYMODULE::MyClass.init(yml)

And here is my my_class.rb:

module MYMODULE
  class MyClass
    def self.init(yml)
      @post_url = yml["defaults"]["post_url"]
    end

    def self.post_url
      @post_url
    end

    # this should be a single instance
    def connection_pool
      # ???
    end
  end
end
  1. These class level variables, how can I access them from both class methods and instance methods?

I’m getting wierd behaviour, and I’m confused as to how to reference the post_url from inside of either class methods and instance methods.

I have seen the following ways, unsure which is correct:

self.class.post_url
MyClass.post_url
@post_url
post_url
self.post_url
  • 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-31T12:28:06+00:00Added an answer on May 31, 2026 at 12:28 pm

    self.class.post_url or MyClass.post_url will work. The difference is how they work for subclasses (in the former case, subclasses will use their own version of this variable automatically, in the latter, they would share the variable in MyClass).

    There is no way to directly access class instance variables from an instance: you have to call a class method which returns (or sets) them. See also: cattr_accessor.

    That said, if this is really a singleton, it seems a little strange to me that you would configure part of it on the class, and then reference that info in the (single) instance. Wouldn’t it make more sense just to configure this stuff on the instance? Or use a module as a singleton and not create an instance at all?

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

Sidebar

Related Questions

I'm getting a little confused how to set up a class that's two steps
I am getting a little confused and need some help please. Take these two
I'm currently learning iOS development and I'm getting confused how titles are being set
I'm getting confused with the include/exclude jargon, and my actual SVN client doesn't seem
I'm getting confused by bank switching in PIC assembler... This works for putting a
I'm getting confused re the range of options for development & deploying some simple
I am getting confused between TCP being Connection oriented and UDP being connectionless so
I'm getting confused in the doco how I should be setting up Ninject. I'm
I'm getting confused by pointers in objective-c. Basically I have a bunch of static
I am getting confused with size_t in C. I know that it is returned

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.