creating objects

Discussion in 'Ruby' started by Felix McCoey, Oct 4, 2005.

  1. Felix McCoey

    Felix McCoey Guest

    i know that in PHP it is possible to do the following:

    $class_name = 'User';
    $$class_name->new()

    but I want to do this in ruby like this perhaps:

    @class_name = 'User'
    @class_name.new

    What is this called and is it possible in ruby. Or is there an
    alternative to creating objects from just the class name?

    -felix
    Felix McCoey, Oct 4, 2005
    #1
    1. Advertising

  2. Felix McCoey

    Hal Fulton Guest

    Felix McCoey wrote:
    > i know that in PHP it is possible to do the following:
    >
    > $class_name = 'User';
    > $$class_name->new()
    >
    > but I want to do this in ruby like this perhaps:
    >
    > @class_name = 'User'
    > @class_name.new
    >
    > What is this called and is it possible in ruby. Or is there an
    > alternative to creating objects from just the class name?


    You can always use const_get:

    class_name = 'User'
    klass = Object.const_get(class_name)
    obj = klass.new


    Hope this helps...


    Hal
    Hal Fulton, Oct 4, 2005
    #2
    1. Advertising

  3. --Apple-Mail-10--222254668
    Content-Transfer-Encoding: 7bit
    Content-Type: text/plain;
    charset=US-ASCII;
    delsp=yes;
    format=flowed


    On Oct 3, 2005, at 11:45 PM, Hal Fulton wrote:

    > Felix McCoey wrote:
    >
    >> i know that in PHP it is possible to do the following:
    >> $class_name = 'User';
    >> $$class_name->new()
    >> but I want to do this in ruby like this perhaps:
    >> @class_name = 'User'
    >> @class_name.new
    >> What is this called and is it possible in ruby. Or is there an
    >> alternative to creating objects from just the class name?
    >>

    >
    > You can always use const_get:
    >
    > class_name = 'User'
    > klass = Object.const_get(class_name)
    > obj = klass.new


    Also, if you can create a string of code that does what you want, you
    can always eval() it:

    obj = eval("#{class_name}.new")

    But the const_get solution is better. It works because classes are
    live objects that are stored in global constants.

    Josh

    --Apple-Mail-10--222254668--
    Joshua Haberman, Oct 4, 2005
    #3
  4. Felix McCoey

    Adam Shelly Guest

    On 10/3/05, Hal Fulton <> wrote:
    > Felix McCoey wrote:
    > > i know that in PHP it is possible to do the following:
    > >
    > > $class_name =3D 'User';
    > > $$class_name->new()
    > >
    > > but I want to do this in ruby like this perhaps:
    > >
    > > @class_name =3D 'User'
    > > @class_name.new
    > >


    @class_name =3D User
    @class_name.new

    -or-

    @class_name =3D "User"
    eval @class_name+".new"
    Adam Shelly, Oct 4, 2005
    #4
    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. bigbinc
    Replies:
    3
    Views:
    388
    Michael Borgwardt
    Nov 18, 2003
  2. Simon Elliott

    Inheritance of objects within objects

    Simon Elliott, Dec 10, 2004, in forum: C++
    Replies:
    2
    Views:
    322
    Simon Elliott
    Dec 10, 2004
  3. =?Utf-8?B?c3RldmVuIHNjYWlmZQ==?=

    form objects into class objects

    =?Utf-8?B?c3RldmVuIHNjYWlmZQ==?=, Jul 5, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    424
    =?Utf-8?B?c3RldmVuIHNjYWlmZQ==?=
    Jul 6, 2006
  4. 7stud
    Replies:
    11
    Views:
    668
    Dennis Lee Bieber
    Mar 20, 2007
  5. News123
    Replies:
    7
    Views:
    368
    Dave Angel
    Jun 29, 2009
Loading...

Share This Page