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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:53:26+00:00 2026-06-14T02:53:26+00:00

I need to load a YAML file (I’m experimenting with SettingsLogic) and I’d like

  • 0

I need to load a YAML file (I’m experimenting with SettingsLogic) and I’d like the instance to load the YAML with the same name as it. Briefly:

class MySettings < SettingsLogic
  source "whatever_the_instance_is_called.yml"

# Do some other stuff here
end

basic_config = MySettings.new    # loads & parses basic_config.yml
advanced_cfg = MySettings.new    # loads & parses advanced_cfg.yml
...and so on...

The reason for this I don’t yet know what configuration files I’ll have to load, and typing:

my_config = MySettings.new("my_config.yml") 

or

my_config = MySettings.new(:MyConfig) 

just seems to be repeating myself.

I took a look around both Google and Stackoverflow, and the closest I came to an answer is either "Get Instance Name" or a discussion about how meaningless an instance name is! (I’m probably getting the query wrong, however.)

I have tried instance#class, and instance#name; I also tried instance#_id2ref(self).

What am I missing?!

Thanks in advance!

  • 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-06-14T02:53:28+00:00Added an answer on June 14, 2026 at 2:53 am

    O.K., so with local variable assignment, there are snags, such as that assignment might occur slightly later than local variable symbol addition to the local variable list. But here is my module ConstMagicErsatz that I used to implement something similar to out-of-the box Ruby constant magic:

    a = Class.new
    a.name #=> nil - anonymous
    ABC = a # constant magic at work
    a.name #=> "ABC"
    

    The advantage here is that you don’t have to write ABC = Class.new( name: “ABC” ), name gets assigned ‘magically’. This also works with Struct class:

    Koko = Struct.new
    Koko.name #=> "Koko"
    

    but with no other classes. So here goes my ConstMagicErsatz that allows you to do

    class MySettings < SettingsLogic
      include ConstMagicErsatz
    end
    
    ABC = MySettings.new
    ABC.name #=> "ABC"
    

    As well as

    a = MySettings.new name: "ABC"
    a.name #=> "ABC"
    

    Here it goes:

    module ConstMagicErsatz
      def self.included receiver
        receiver.class_variable_set :@@instances, Hash.new
        receiver.class_variable_set :@@nameless_instances, Array.new
        receiver.extend ConstMagicClassMethods
      end
    
      # The receiver class will obtain #name pseudo getter method.
      def name
        self.class.const_magic
        name_string = self.class.instances[ self ].to_s
        name_string.nil? ? nil : name_string.demodulize
      end
    
      # The receiver class will obtain #name setter method
      def name= ɴ
        self.class.const_magic
        self.class.instances[ self ] = ɴ.to_s
      end
    
      module ConstMagicClassMethods
        # #new method will consume either:
        # 1. any parameter named :name or :ɴ from among the named parameters,
        # or,
        # 2. the first parameter from among the ordered parameters,
        # and invoke #new of the receiver class with the remaining arguments.
        def new( *args, &block )
          oo = args.extract_options!
          # consume :name named argument if it was supplied
          ɴς = if oo[:name] then oo.delete( :name ).to_s
               elsif oo[:ɴ] then oo.delete( :ɴ ).to_s
               else nil end
          # but do not consume the first ordered argument
          # and call #new method of the receiver class with the remaining args:
          instance = super *args, oo, &block
          # having obtained the instance, attach the name to it
          instances.merge!( instance => ɴς )
          return instance
        end
    
        # The method will search the namespace for constants to which the objects
        # of the receiver class, that are so far nameless, are assigned, and name
        # them by the first such constant found. The method returns the number of
        # remaining nameless instances.
        def const_magic
          self.nameless_instances = 
            class_variable_get( :@@instances ).select{ |key, val| val.null? }.keys
          return 0 if nameless_instances.size == 0
          catch :no_nameless_instances do search_namespace_and_subspaces Object end
          return nameless_instances.size
        end # def const_magic
    
        # @@instances getter and setter for the target class
        def instances; const_magic; class_variable_get :@@instances end
        def instances= val; class_variable_set :@@instances, val end
    
        # @@nameless_instances getter for the target class
        def nameless_instances; class_variable_get :@@nameless_instances end
        def nameless_instances= val; class_variable_set :@@nameless_instances, val end
    
        private
    
        # Checks all the constants in some module's namespace, recursivy
        def search_namespace_and_subspaces( ɱodule, occupied = [] )
          occupied << ɱodule.object_id           # mark the module "occupied"
    
          # Get all the constants of ɱodule namespace (in reverse - more effic.)
          const_symbols = ɱodule.constants( false ).reverse
    
          # check contents of these constant for wanted objects
          const_symbols.each do |sym|
            # puts "#{ɱodule}::#{sym}" # DEBUG
            # get the constant contents
            obj = ɱodule.const_get( sym ) rescue nil
            # is it a wanted object?
            if nameless_instances.map( &:object_id ).include? obj.object_id then
              class_variable_get( :@@instances )[ obj ] = ɱodule.name + "::#{sym}"
              nameless_instances.delete obj
              # and stop working in case there are no more unnamed instances
              throw :no_nameless_instances if nameless_instances.empty?
            end
          end
    
          # and recursively descend into the subspaces
          const_symbols.each do |sym|
            obj = ɱodule.const_get sym rescue nil # get the const value
            search_namespace_and_subspaces( obj, occupied ) unless
              occupied.include? obj.object_id if obj.kind_of? Module
          end
        end
      end # module ConstMagicClassMethods
    end # module ConstMagicErsatz
    

    The above code implements automatic searching of whole Ruby namespace with the aim of finding which constant refers to the given instance, whenever #name method is called.

    The only constraint using constants gives you, is that you have to capitalize it. Of course, what you want would be modifying the metaclass of the object after it is already born and assigned to a constant. Since, again, there is no hook, you have to finde the occasion to do this, such as when the new object is first used for its purpose. So, having

    ABC = MySettings.new
    

    and then, when the first use of your MySettings instance occurs, before doing anything else, to patch its metaclass:

    class MySettings
      def do_something_useful
        # before doing it
        instance_name = self.name
        singleton_class.class_exec { source "#{instance_name}.yml" }
      end
    
      # do other useful things
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to load a yaml file into Hash, What should I do?
I need to load a custom function from an extern file but without causing
I need to load a .xml file from a URL adress into an NSData
I need to load a file from an umounted TrueCrypt disk into memory. Is
I need load image from assets I can read text file but i can
I need to open a YAML file with aliases used inside it: defaults: &defaults
I need to load the jQuery UI files, and would like to do it
Need to load usercontrols to my form dynamically. I have menu and passing name
I have a test backend defined like this in my backends.yaml backends: - name:
I need load external resources from another server like css, template, data... but I

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.