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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:58:51+00:00 2026-05-23T13:58:51+00:00

I am writing an internal DSL in Ruby. For this, I need to programmatically

  • 0

I am writing an internal DSL in Ruby. For this, I need to programmatically create named classes and nested classes. What is the best way to do so? I recon that there are two ways to do so:

  1. Use Class.new to create an anonymous class, then use define_method to add methods to it, and finally call const_set to add them as named constants to some namespace.
  2. Use some sort of eval

I’ve tested the first way and it worked, but being new to Ruby, I am not sure that putting classes as constants is the right way.

Are there other, better ways? If not, which of the above is preferable?

  • 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-23T13:58:51+00:00Added an answer on May 23, 2026 at 1:58 pm

    If you want to create a class with a dynamic name, you’ll have to do almost exactly what you said. However, you do not need to use define_method. You can just pass a block to Class.new in which you initialize the class. This is semantically identical to the contents of class/end.

    Remember with const_set, to be conscientious of the receiver (self) in that scope. If you want the class defined globally you will need to call const_set on the TopLevel module (which varies in name and detail by Ruby).

    a_new_class = Class.new(Object) do
      attr_accessor :x
    
      def initialize(x)
        print #{self.class} initialized with #{x}"
        @x = x
      end
    end
    
    SomeModule.const_set("ClassName", a_new_class)
    
    c = ClassName.new(10)
    
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Writing my first Linq application, and I'm trying to find the best way to
I'm writing an internal service that needs to touch a mod_perl2 instance for a
I'm writing an internal-tools webapp; one of the central pages in this tool has
I'm writing an internal business application that needs to access a number of different
I'm writing a parser for an internal xml-based metadata format in python. I need
I use this LINQ query in an app I am writing: internal int GetNoteCount(DateTime
I am involved in writing a little internal SharePoint portal for our company. I
Writing a JSP page, what exactly does the <c:out> do? I've noticed that the
Writing something like this using the loki library , typedef Functor<void> BitButtonPushHandler; throws a
I writing a report in Visual Studio that takes a user input parameter and

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.