Syntax for <stringVariable>.new ?

L

Larry Fast

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
 
S

Sebastian Hungerecker

Larry said:
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
 
M

MenTaLguY

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
 
M

MenTaLguY

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
 
M

mortee

MenTaLguY said:
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
 
L

Larry Fast

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
 
W

Wilson Bilkovich

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
 
7

7stud --

Sebastian said:
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.
 
C

Chris Carter

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.

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

Phrogz

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

Wilson Bilkovich

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

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

Phrogz

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

Wilson Bilkovich

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.

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

David A. Black

Hi --

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,264
Messages
2,571,336
Members
48,014
Latest member
saradhi

Latest Threads

Top