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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:09:44+00:00 2026-05-27T20:09:44+00:00

I’ve been practicing some Ruby meta-programming recently, and was wondering about assigning anonymous classes

  • 0

I’ve been practicing some Ruby meta-programming recently, and was wondering about assigning anonymous classes to constants.

In Ruby, it is possible to create an anonymous class as follows:

anonymous_class = Class.new  # => #<Class:0x007f9c5afb21d0>

New instances of this class can be created:

an_instance = anonymous_class.new # => #<#<Class:0x007f9c5afb21d0>:0x007f9c5afb0330>

Now, when the anonymous class is assigned to a constant, the class now has a proper name:

Foo = anonymous_class # => Foo

And the previously created instance is now also an instance of that class:

an_instance # => #<Foo:0x007f9c5afb0330>

My question: Is there a hook method for the moment when an anonymous class is assigned to a constant?

There are many hooks methods in Ruby, but I couldn’t find this one.

  • 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-27T20:09:44+00:00Added an answer on May 27, 2026 at 8:09 pm

    Let’s take a look at how constant assignment works internally. The code that follows is extracted from a source tarball of ruby-1.9.3-p0. First we look at the definition of the VM instruction setconstant (which is used to assign constants):

    # /insns.def, line 239
    DEFINE_INSN
    setconstant
    (ID id)
    (VALUE val, VALUE cbase)
    ()
    {
      vm_check_if_namespace(cbase);
      rb_const_set(cbase, id, val);
      INC_VM_STATE_VERSION();
    }
    

    No chance to place a hook in vm_check_if_namespace or INC_VM_STATE_VERSION here. So we look at rb_const_set (variable.c:1886), the function that is called everytime a constant is assigned:

    # /variable.c, line 1886
    void
    rb_const_set(VALUE klass, ID id, VALUE val)
    {
        rb_const_entry_t *ce;
        VALUE visibility = CONST_PUBLIC;
    
        # ...
    
        check_before_mod_set(klass, id, val, "constant");
        if (!RCLASS_CONST_TBL(klass)) {
          RCLASS_CONST_TBL(klass) = st_init_numtable();
        }
        else {
          # [snip], won't be called on first assignment
        }
    
        rb_vm_change_state();
    
        ce = ALLOC(rb_const_entry_t);
        ce->flag = (rb_const_flag_t)visibility;
        ce->value = val;
    
        st_insert(RCLASS_CONST_TBL(klass), (st_data_t)id, (st_data_t)ce);
    }
    

    I removed all the code that was not even called the first time a constant was assigned inside a module. I then looked into all the functions called by this one and didn’t find a single point where we could place a hook from Ruby code. This means the hard truth is, unless I missed something, that there is no way to hook a constant assignment (at least in MRI).

    Update

    To clarify: The anonymous class does not magically get a new name as soon as it is assigned (as noted correctly in Andrew’s answer). Rather, the constant name along with the object ID of the class is stored in Ruby’s internal constant lookup table. If, after that, the name of the class is requested, it can now be resolved to a proper name (and not just Class:0xXXXXXXXX...).

    So the best you can do to react to this assignment is to check the name of the class in a loop of a background worker thread until it is non-nil (which is a huge waste of resources, IMHO).

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I have some data like this: 1 2 3 4 5 9 2 6

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.