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|
  <insert object creation code here>
}

Thanks,
Larry

···

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

Larry Fast wrote:

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

classList.each { |klass|
  <insert object creation code here>
}

Object.const_get(klass).new

HTH,
Sebastian

···

--
Jabber: sepp2k@jabber.org
ICQ: 205544826

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

···

On Thu, 27 Sep 2007 06:26:34 +0900, Larry Fast <lfast@mdsi.ca> wrote:

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

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

···

On Thu, 27 Sep 2007 06:32:10 +0900, Sebastian Hungerecker <sepp2k@googlemail.com> wrote:

Object.const_get(klass)

Sebastian Hungerecker 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.

···

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

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

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

···

--
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, 7stud -- <dolgun@excite.com> wrote:

Sebastian Hungerecker 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.
--
Posted via http://www.ruby-forum.com/\.

--
Chris Carter
concentrationstudios.com
brynmawrcs.com

Hi --

···

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

Sebastian Hungerecker 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

--
Upcoming training from Ruby Power and Light, LLC:
   * Intro to Ruby on Rails, Edison, NJ, October 23-26
   * Advancing with Rails, Edison, NJ, November 6-9
Both taught by David A. Black.
See http://www.rubypal.com for more info!

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 9/26/07, Larry Fast <lfast@mdsi.ca> 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.

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 Sep 26, 8:41 pm, "Chris Carter" <cdcar...@gmail.com> 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.

"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'.

···

On 9/26/07, Phrogz <phrogz@mac.com> wrote:

On Sep 26, 8:41 pm, "Chris Carter" <cdcar...@gmail.com> 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.)

                               +------------------+
                               > >
                 Object---->(Object) |
                  ^ ^ ^ ^ |
                  > > > > >
                  > > +-----+ +---------+ |
                  > > > > >
                  > +-----------+ | |
                  > > > > >
           +------+ | 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.

···

On Sep 26, 9:49 pm, "Wilson Bilkovich" <wils...@gmail.com> wrote:

> 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.)

"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) |
                                 ^ |
                                 > >
                                 +----------------+

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.

···

On 9/27/07, Phrogz <phrogz@mac.com> wrote:

On Sep 26, 9:49 pm, "Wilson Bilkovich" <wils...@gmail.com> wrote:
> > 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.)
>
> "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) |
> ^ |
> > >
> +----------------+

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.