Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a class dynamically

I'm trying to create a new class, without knowing the name of the class until it's supposed to be created.

Something like this;

    variable = "ValidClassName"
    
        class variable
    
        end

Test = ValidClassName.new

If possible, i'd also appreciate som hints on how to dynamically add attributes (and methods) to this new class.

I'll be retreiving 'settings' for the class, and they will look something like this:

title :Person
attribute :name, String
attribute :age, Fixnum

But should not be designed to accept only that explicit file, the attributes might differ in number end type.

Which in the end will generate a class that should look something like:

class Person
   def initialize(name, age)
 
       @name_out = name
       @age_out = age
   end
   
end

Help?

like image 548
BSG Avatar asked Feb 22 '12 22:02

BSG


People also ask

How do you create a dynamic class?

Classes can be dynamically created using the type() function in Python. The type() function is used to return the type of the object. The above syntax returns the type of object.

Can we create a class dynamically in C#?

You can create custom dynamic objects by using the classes in the System. Dynamic namespace. For example, you can create an ExpandoObject and specify the members of that object at run time. You can also create your own type that inherits the DynamicObject class.

Can we create a class at runtime?

Dynamic Class creation enables you to create a Java class on the fly at runtime, from source code created from a string. Dynamic class creation can be used in extremely low latency applications to improve performance.

What is dynamic class in C#?

In C# 4.0, a new type is introduced that is known as a dynamic type. It is used to avoid the compile-time type checking. The compiler does not check the type of the dynamic type variable at compile time, instead of this, the compiler gets the type at the run time.


1 Answers

A class gains its name when it is assigned to a constant. So It's easy to do in a generic fashion with const_set.

For example, let's say you want to use Struct to build a class with some attributes, you can:

name = "Person"
attributes = [:name, :age]

klass = Object.const_set name, Struct.new(*attributes)
# Now use klass or Person or const_get(name) to refer to your class:
Person.new("John Doe", 42) # => #<struct Person name="John Doe", age=42>

To inherit from another class, replace the Struct.new by Class.new(MyBaseClass), say:

class MyBaseClass; end

klass = Class.new(MyBaseClass) do
  ATTRIBUTES = attributes
  attr_accessor *ATTRIBUTES
  def initialize(*args)
    raise ArgumentError, "Too many arguments" if args.size > ATTRIBUTES.size
    ATTRIBUTES.zip(args) do |attr, val|
      send "#{attr}=", val
    end
  end
end
Object.const_set name, klass
Person.new("John Doe", 42) # => #<Person:0x007f934a975830 @name="John Doe", @age=42> 
like image 144
Marc-André Lafortune Avatar answered Sep 19 '22 06:09

Marc-André Lafortune