Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Class Instantiation

Reply
Thread Tools

Class Instantiation

 
 
jantzeno
Guest
Posts: n/a
 
      08-14-2007
I have a few questions about class instantiation.

Say I have a class:

class Person
attr_accessor :name, :age
end

And an array:

names = ["john", "jane"]

Is it possible to instantiate a class using a string from the array so
I get something equal to:

john = Person.new
jane = Person.new


v/r,


 
Reply With Quote
 
 
 
 
Douglas F Shearer
Guest
Posts: n/a
 
      08-14-2007
On 14 Aug 2007, at 22:24, jantzeno wrote:
>
> Is it possible to instantiate a class using a string from the array so


names.each do |n|
eval n + ' = Person.new'
end

I'm sure there is a neater solution to this, but I'll put this
forward for now.

Douglas F Shearer
http://www.velocityreviews.com/forums/(E-Mail Removed)
http://douglasfshearer.com



 
Reply With Quote
 
 
 
 
Todd Burch
Guest
Posts: n/a
 
      08-14-2007
jantzeno wrote:
> I have a few questions about class instantiation.
>
> Say I have a class:
>
> class Person
> attr_accessor :name, :age
> end
>
> And an array:
>
> names = ["john", "jane"]
>
> Is it possible to instantiate a class using a string from the array so
> I get something equal to:
>
> john = Person.new
> jane = Person.new
>
>
> v/r,


You can, but it might be better to do something like this if you have
lots of names in your names array:

people = Array.new ;

names.each_with_index {|n,i|
people[i] = Person.new ;
people[i].name = n ;
}

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

 
Reply With Quote
 
dblack@rubypal.com
Guest
Posts: n/a
 
      08-14-2007
Hi --

On Wed, 15 Aug 2007, Douglas F Shearer wrote:

> On 14 Aug 2007, at 22:24, jantzeno wrote:
>>
>> Is it possible to instantiate a class using a string from the array so

>
> names.each do |n|
> eval n + ' = Person.new'
> end


That won't work, both because it's inside a block:

1.times do
david = 1
end

p david # error -- undefined

and because eval creates its own binding for local variable
assignments:

eval("a = 1")
p a

For both reasons, the variable would already have to be in view before
the eval.

The best and most common advice given in response to this question is:
do it with a hash instead, like this:

people = {}
names.each {|name| people[name] = Person.new }


David

--
* Books:
RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
& consulting: Ruby Power and Light, LLC (http://www.rubypal.com)

 
Reply With Quote
 
jantzeno
Guest
Posts: n/a
 
      08-14-2007

>
> The best and most common advice given in response to this question is:
> do it with a hash instead, like this:
>
> people = {}
> names.each {|name| people[name] = Person.new }
>


This was going to be my next question.

Then I can do a:

people["john"].name = "john"

Brilliant, thanks.


 
Reply With Quote
 
Steve Austen
Guest
Posts: n/a
 
      11-29-2010
hi its seems like you have done a hard work on it. I have got lots of

information from your post. Really appreciate your work.!! It was

describe very nicely keep us doing good work..
http://www.dealsourcedirect.com/ion-tape2pc.html

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally. krunalbauskar@gmail.com C++ 1 12-25-2006 03:51 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Set Class Instance to Nothing During Instantiation? Jeff Carver ASP .Net 3 08-25-2004 12:10 PM
JSP problem with classpath and class instantiation ... d wood Java 1 04-15-2004 10:18 PM
Re: Class instantiation puzzle Ike Java 0 06-27-2003 11:02 AM



Advertisments