On Wed, Sep 28, 2005 at 06:42:57AM +0900, Jonas Galvez wrote:
> In Python I can do this:
> 
> >>> def create_class(name):
> ...     import new
> ...     c = new.classobj(name, tuple([object]), {})
> ...     def __init__(self, value):
> ...         self.value = value
> ...     setattr(c, "__init__", new.instancemethod(__init__, None, c))
> ...     return c
> ...
> >>> MyClass = create_class("MyClass")
> >>>
> >>> obj = MyClass(value=10)
> >>> print obj.value
> 10
> 
> Is there anything similar in Ruby? Or do I need to use eval()?

    MyClass = Class.new do
      attr_accessor :value
      def initialize(value)
        @value = value
      end
    end

    obj = MyClass.new(10)
    puts obj.value

Cleaner, I think =).

If you want "MyClass" to be dynamically set:
    Obj.const_set("MyClass",Class.new { ... })

Or for really simple classes, you can use 'struct':

    require 'struct'
    Struct.new('MyClass','value')
    obj = MyClass.new(10)
    puts obj.value

Hope that helps!

- Greg