[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Syntax for .new ?

Larry Fast

9/26/2007 9:27:00 PM

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

13 Answers

Sebastian Hungerecker

9/26/2007 9:32:00 PM

0

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

MenTaLguY

9/26/2007 9:35:00 PM

0

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?

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


MenTaLguY

9/26/2007 9:39:00 PM

0

On Thu, 27 Sep 2007 06:32:10 +0900, Sebastian Hungerecker <sepp2k@googlemail.com> 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


mortee

9/26/2007 10:33:00 PM

0

Larry Fast

9/27/2007 1:53:00 AM

0

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

Wilson Bilkovich

9/27/2007 2:19:00 AM

0

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

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

7stud 7stud

9/27/2007 2:21:00 AM

0

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

Chris Carter

9/27/2007 2:42:00 AM

0

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

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.
--
Chris Carter
concentrationstudios.com
brynmawrcs.com

Phrogz

9/27/2007 3:19:00 AM

0

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/RubyMethodLook...

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

Wilson Bilkovich

9/27/2007 3:50:00 AM

0

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