Class Instantiation

Discussion in 'Ruby' started by jantzeno, Aug 14, 2007.

  1. jantzeno

    jantzeno Guest

    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,
     
    jantzeno, Aug 14, 2007
    #1
    1. Advertising

  2. 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://douglasfshearer.com
     
    Douglas F Shearer, Aug 14, 2007
    #2
    1. Advertising

  3. jantzeno

    Todd Burch Guest

    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 = Person.new ;
    people.name = n ;
    }

    Todd
    --
    Posted via http://www.ruby-forum.com/.
     
    Todd Burch, Aug 14, 2007
    #3
  4. jantzeno

    Guest

    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)
     
    , Aug 14, 2007
    #4
  5. jantzeno

    jantzeno Guest


    >
    > 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.
     
    jantzeno, Aug 14, 2007
    #5
  6. jantzeno

    Steve Austen Guest

    Steve Austen, Nov 29, 2010
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Carver
    Replies:
    3
    Views:
    475
    Jeff Carver
    Aug 25, 2004
  2. Ike
    Replies:
    0
    Views:
    1,336
  3. d wood
    Replies:
    1
    Views:
    566
    Christophe Vanfleteren
    Apr 15, 2004
  4. E11
    Replies:
    1
    Views:
    4,958
    Thomas Weidenfeller
    Oct 12, 2005
  5. Replies:
    1
    Views:
    621
    Salt_Peter
    Dec 25, 2006
Loading...

Share This Page