Syntax for <stringVariable>.new?

Hi Rubyists and …istas,
I want create objects from a list of class names. What’s the syntax for
this?

classList.each { |klass|

}

Thanks,
Larry

Larry F. wrote:

Hi Rubyists and …istas,
I want create objects from a list of class names. What’s the syntax for
this?

classList.each { |klass|

}

Object.const_get(klass).new

HTH,
Sebastian

On Thu, 27 Sep 2007 06:32:10 +0900, Sebastian H.
[email protected] wrote:

Object.const_get(klass)

Note that this only works for top-level classes; “Foo”, but not
“Bar::Baz”. The general way to obtain class objects by name is
this:

name.split("::").inject(Object) { |c, n| c.const_get(n) }

-mental

MenTaLguY wrote:

The general way to obtain class objects by name is
this:

name.split("::").inject(Object) { |c, n| c.const_get(n) }

What about

eval(className).new

?

mortee

On Thu, 27 Sep 2007 06:26:34 +0900, Larry F. [email protected] wrote:

I want create objects from a list of class names. What’s the syntax for
this?

You usually don’t need to go via class names; for example this works
fine:

class Foo

end

class Bar

end

classes = [ Foo, Bar ]

Classes are themselves objects in Ruby, so if you have a class you can
simply call the #new method on it.

instances = classes.map { |c| c.new }

-mental

Thank you all. This version was all I needed:
Object.const_get(klass)
I’m receiving only the child-class name with none of it’s parentage.

But now I’m trying to curious about how Test::Unit does this job. My
first kick at this problem was to find out how it was done there. I
still haven’t found anything that looks like Object instantiation in the
Test::Unit code.

Cheers,
Larry

Sebastian H. wrote:

Object.const_get(klass).new

I can’t find any documentation for Object.const_get. ri reveals nothing
and pickaxe2 doesn’t have anything for Object.const_get, although it
does list something for Module#const_get. pickaxe2 says that Object
mixes in Kernel, but I can’t find anything for Kernel.const_get either.

On 9/26/07, 7stud – [email protected] wrote:

Posted via http://www.ruby-forum.com/.

Kernel is a module, so it gets the Module instance methods, and Object
(and thus all objects) mix in Kernel, so they have const_get. The
Module<->Class<->Object<->Kernel quartet is pretty magic in Ruby.

On 9/26/07, Larry F. [email protected] wrote:

Thank you all. This version was all I needed:
Object.const_get(klass)
I’m receiving only the child-class name with none of it’s parentage.

But now I’m trying to curious about how Test::Unit does this job. My
first kick at this problem was to find out how it was done there. I
still haven’t found anything that looks like Object instantiation in the
Test::Unit code.

Test::Unit uses ObjectSpace shiver to do its work, at the moment.
In my opinion, a better way to do it would be:
class Test::Unit::TestCase
def self.inherited(subclass)
@test_cases ||= []
@test_cases << subclass
end
def self.test_cases
@test_cases || []
end
end

That way it would collect up a list of classes as they were defined, and
then:
at_exit do
Test::Unit::TestCase.test_cases.each {|tc| tc.run_thyself }
end

On Sep 26, 8:41 pm, “Chris C.” [email protected] wrote:

Kernel is a module, so it gets the Module instance methods, and Object
(and thus all objects) mix in Kernel, so they have const_get. The
Module<->Class<->Object<->Kernel quartet is pretty magic in Ruby.

I don’t believe this is correct. Object.const_get works because Object
is a class, and therefore an instance of the Class class, which
inherits from the Module class.

See http://phrogz.net/RubyLibs/RubyMethodLookupFlow.png

(There’s no official guarantee that the diagram above is correct, but
I have not yet been able to find fault with it, and it has been
reviewed and had corrections made thanks to some people quite
familiar with Ruby.)

On 9/26/07, Phrogz [email protected] wrote:

(There’s no official guarantee that the diagram above is correct, but
I have not yet been able to find fault with it, and it has been
reviewed and had corrections made thanks to some people quite
familiar with Ruby.)

“ri Class” also has a nice simple ASCII diagram that can be more
approachable for new people.

 Classes, modules, and objects are interrelated. In the diagram
 that follows, the vertical arrows represent inheritance, and the
 parentheses meta-classes. All metaclasses are instances of the
 class `Class'.

                           +------------------+
                           |                  |
             Object---->(Object)              |
              ^  ^        ^  ^                |
              |  |        |  |                |
              |  |  +-----+  +---------+      |
              |  |  |                  |      |
              |  +-----------+         |      |
              |     |        |         |      |
       +------+     |     Module--->(Module)  |
       |            |        ^         ^      |
  OtherClass-->(OtherClass)  |         |      |
                             |         |      |
                           Class---->(Class)  |
                             ^                |
                             |                |
                             +----------------+

On Sep 26, 9:49 pm, “Wilson B.” [email protected] wrote:

 Classes, modules, and objects are interrelated. In the diagram
              |  |  |                  |      |
              |  +-----------+         |      |
              |     |        |         |      |
       +------+     |     Module--->(Module)  |
       |            |        ^         ^      |
  OtherClass-->(OtherClass)  |         |      |
                             |         |      |
                           Class---->(Class)  |
                             ^                |
                             |                |
                             +----------------+

Interesting that you would characterize it as such. It’s certainly
both official and correct (and was definitely helpful in creating my
own version), but I personally found it wholly confusing. It was only
through a fair amount of experimentation, repeatedly re-reading the
explanation (what’s a ‘meta-class’ again?), and squinting my eyes at
the diagram that I was able to decipher it.

But, if that diagram does it for you, all the better. It certainly has
less boxes and lines.

Hi –

On Thu, 27 Sep 2007, 7stud – wrote:

Sebastian H. wrote:

Object.const_get(klass).new

I can’t find any documentation for Object.const_get. ri reveals nothing
and pickaxe2 doesn’t have anything for Object.const_get, although it
does list something for Module#const_get. pickaxe2 says that Object
mixes in Kernel, but I can’t find anything for Kernel.const_get either.

Module#const_get is indeed what you want. Object is just the receiver
of the message in this particular case. #const_get is an instance
method of Module.

David

On 9/27/07, Phrogz [email protected] wrote:

              |  |  +-----+  +---------+      |
                             +----------------+

I guess that one is clear to me because it quickly shows “everything
has a metaclass”, after which you can pretty much ignore anything in
parens. Heh.

On the other hand, maybe I’m just mis-remembering how I actually
learned the structure when I first picked Ruby up.