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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:16:56+00:00 2026-05-11T08:16:56+00:00

I have a class that should look something like this: class Family_Type1 @people =

  • 0

I have a class that should look something like this:

class Family_Type1     @people = Array.new(3)     @people[0] = Policeman.new('Peter', 0)     @people[1] = Accountant.new('Paul', 0)     @people[2] = Policeman.new('Mary', 0)      def initialize(*ages)         for i in 0 ... @people.length             @people[i].age = ages[i]         end     end end 

I want to be able to define a bunch of classes similar to this one at runtime (define them once at startup) where the size of the array and the type assigned to each parameter is defined at runtime from an external specification file.

I sort of got it to work using evals but this is really ugly. Any better way?

  • 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. 2026-05-11T08:16:56+00:00Added an answer on May 11, 2026 at 8:16 am

    First off, part of the reason your example code isn’t working for you is that you have two different @people variables – one is an instance variable and the other is a class instance variable.

    class Example   # we're in the context of the Example class, so    # instance variables used here belong to the actual class object,   # not instances of that class   self.class #=> Class   self == Example #=> true   @iv = 'I'm a class instance variable'    def initialize     # within instance methods, we're in the context     # of an _instance_ of the Example class, so     # instance variables used here belong to that instance.     self.class #=> Example     self == Example #=> false     @iv = 'I'm an instance variable'   end   def iv     # another instance method uses the context of the instance     @iv #=> 'I'm an instance variable'   end   def self.iv     # a class method, uses the context of the class     @iv #=> 'I'm a class instance variable'   end end 

    If you want to create variables one time in a class to use in instance methods of that class, use constants or class variables.

    class Example   # ruby constants start with a capital letter.  Ruby prints warnings if you   # try to assign a different object to an already-defined constant   CONSTANT_VARIABLE = 'i'm a constant'   # though it's legit to modify the current object   CONSTANT_VARIABLE.capitalize!   CONSTANT_VARIABLE #=> 'I'm a constant'    # class variables start with a @@   @@class_variable = 'I'm a class variable'    def c_and_c     [ @@class_variable, CONSTANT_VARIABLE ] #=> [ 'I'm a class variable', 'I'm a constant' ]   end end 

    Even so, in the context of your code, you probably don’t want all your instances of Family_Type1 to refer to the same Policemen and Accountants right? Or do you?

    If we switch to using class variables:

    class Family_Type1     # since we're initializing @@people one time, that means     # all the Family_Type1 objects will share the same people     @@people = [ Policeman.new('Peter', 0), Accountant.new('Paul', 0), Policeman.new('Mary', 0) ]      def initialize(*ages)         @@people.zip(ages).each { |person, age| person.age = age }     end     # just an accessor method     def [](person_index)       @@people[person_index]     end end fam = Family_Type1.new( 12, 13, 14 ) fam[0].age == 12 #=> true # this can lead to unexpected side-effects  fam2 = Family_Type1.new( 31, 32, 29 ) fam[0].age == 12 #=> false fam2[0].age == 31 #=> true fam[0].age == 31 #=> true 

    The runtime initialization can be done with metaprogramming, as Chirantan said, but if you are only initializing a few classes, and you know what their name is, you can also do it just by using whatever you read from the file:

    PARAMS = File.read('params.csv').split('\n').map { |line| line.split(',') } make_people = proc do |klasses, params|   klasses.zip(params).map { |klass,name| klass.new(name, 0) } end class Example0   @@people = make_people([ Fireman, Accountant, Fireman ], PARAMS[0]) end class Example1   @@people = make_people([ Butcher, Baker, Candlestickmaker ], PARAMS[0]) end 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a base class that I want to look like this: class B
I have Hibernate Entities that look something like this (getters and setters left out):
I have a Django model that looks something like this: class Person(models.Model): name =
Scala programmer should have known that this sort of writing : class Person{ var
I have class that looks like this: class A { public: class variables_map vm
I have a model that looks like this: public class Book { public string
Lets say I have a custom data type that looks something like this: public
I have a helper class that should only ever run in a background thread.
I have had a few occasions where something like this would be helpful. I
I have an IEnumerable of an item class defined like this: public class Item

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.