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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:54:05+00:00 2026-06-03T03:54:05+00:00

I’m writing a little piece of code in Ruby (1.9.3), and I use a

  • 0

I’m writing a little piece of code in Ruby (1.9.3), and I use a pair of simple “enum-like” classes, that define some constants with const_set and some behavior of these constants (e.g. the class Days may have the constants MON, TUE… and Days::MON.succ should evaluate to TUE).

I’m really comfortable with these classes. However, while growing my code, i sometimes need to add more of them, and I don’t like the idea of having five or more classes that share 99% of source code, e.g.:

class Days
  NAMES = %w( MON TUE ... )
  INSTANCES = []

  def initialize(num)
    @num = num
  end

  # An example operation
  def +(n)
    INSTANCES[(@num + n) % INSTANCES.length]
  end

  # Another example operation
  def succ
    self + 1
  end

  def to_s
    NAMES[@num]
  end

  NAMES.each_with_index do |name, idx|
    instance = new(idx)
    INSTANCES[idx] = instance
    const_set name, instance
  end
end

class Months
  NAMES = %w( JAN FEB ... )
  ...
end

I was wondering if Ruby’s metaprogramming capabilities could be used to generate these classes. However, I’m having an hard time creating NAMES, INSTANCES and the “enum-named” constants (e.g. MON, TUE, …).
Being const_set a class method of Class, in this code it’s context (the value of self) is respectively Days and Months.

When creating a factory method, I’m compelled to do something like this:

def enum_new(names_array)
  Class.new do
    const_set "NAMES" []
    names_array.each_with_index do |name, idx|
      NAMES[idx] = name
    end
     ...
  end
end
Days = enum_new(%w| MON TUE ... |)
Months = enum_new(%w| JAN FEB ... |)

but this won’t work (at least, not like i hoped it to), because const_set won’t be called in the context of the class whose name is magically set (i.e. Days and Months), but apparently in the context of Class; therefore, not only it won’t be accessible from the instance methods, but it will be overwrited every time enum_new is called with a new array of names as argument. A similar problem shows up when using class variables, because they’ll be shared between any class generated with the method (because they’ll become class variables of Class, i guess).

Is there any way to create constants in a class generated with Class.new, obtaining this way classes identical in everything to the original Days and Months classes, without having to pollute my code with almost identical classes?

Thanks for your attention and patience! 🙂

  • 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-03T03:54:05+00:00Added an answer on June 3, 2026 at 3:54 am

    Yes. Do your initialization in a class_exec block, into which you can pass your name data and where self refers to the right class:

    theClass=Class.new
    theClass.class_exec(names) do |names|
      #initialize constants here...
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

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’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string

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.