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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:05:18+00:00 2026-06-04T08:05:18+00:00

I’ve been trying to set up a system whereby I can generate a series

  • 0

I’ve been trying to set up a system whereby I can generate a series of similar Ruby classes, distinguished by an integer parameter, which I save into a class variable of the relevant class – something akin to C++ templates.

However, referencing (hence, creating) a new version of the templated class overwrites the saved parameters in the previous versions, and I can’t work out why.

Here’s a minimal example

class Object
  def self.const_missing(name)
    if name =~ /^Templ(\d+)$/
      return make_templ $1.to_i
    else
      raise NameError.new("uninitialised constant #{name}")
    end
  end

private
  def make_templ(base)
    # Make sure we don't define twice
    if Object.const_defined? "Templ#{base}"
      return Object.const_get "Templ#{base}"
    else
      # Define a stub class
      Object.class_eval "class Templ#{base}; end"

      # Open the class and define the actual things we need.
      Object.const_get("Templ#{base}").class_exec(base) do |in_base|        
        @@base = in_base

        def initialize
          puts "Inited with base == #{@@base}"
        end
      end

      Object.const_get("Templ#{base}")
    end
  end
end

irb(main):002:0> Templ1.new
Inited with base == 1
=> #<Templ1:0x26c11c8>
irb(main):003:0> Templ2.new
Inited with base == 2
=> #<Templ2:0x20a8370>
irb(main):004:0> Templ1.new
Inited with base == 2
=> #<Templ1:0x261d908>

Have I found a bug in my Ruby (ruby 1.9.2p290 (2011-07-09) [i386-mingw32]), or have I simply coded something wrong?

  • 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-04T08:05:20+00:00Added an answer on June 4, 2026 at 8:05 am

    Because you first syntactically reference @@base in the context of class Object, it’s a class variable of Object and all the TemplX subclasses of object refer to the superclass’s class var. You can change your code to use Module#class_variable_set and class_variable_get to avoid the binding in the superclass.

    A few other issues with your code: I note you didn’t make make_templ a class method peer of self.const_missing, though it dispatched successfully because Object is an ancestor of Class. It’s best to avoid all forms of eval(string) when other methods exist. You shouldn’t raise NameError if you don’t handle the const_missing, but rather dispatch to super as someone else may be in the chain and want to do something to resolve the constant.

    class Object
      def self.const_missing(name)
        if name =~ /^Templ(\d+)$/
          return make_templ $1.to_i
        end
        super
      end
    
    private
      def self.make_templ(base)
        klass_name = "Templ#{base}"
        unless const_defined? klass_name
          klass = Class.new(Object) do
            class_variable_set :@@base, base
            def initialize
              puts "Inited with base == #{self.class.class_variable_get(:@@base)}"
            end
          end
          const_set klass_name, klass    
        end
    
        const_get klass_name
      end
    end
    

    Class variables have interesting and often undesirable information mixing properties through inheritance. You’ve hit one of the gotchas. I don’t know what other properties you need around @@base, but it looks likely that you’ll get better isolation and less suprising results using a class instance variable instead. For more explanation: Fowler, RailsTips

    • 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 am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

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.