SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2001
    Location
    North Wales, UK
    Posts
    181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Instantiating model from variable?

    Hi all, a quick query if you could:

    How do I instantiate a new model when I have the model name in a variable? In PHP it'd be something like this:

    PHP Code:
    $modelName 'Person';
    $person = new $modelName(); 
    so in Ruby I think I want something like:

    Code:
    modelName = 'Person'
    @person = #{modelName}.new
    but this doesn't work. Any pointers appreciated!

  2. #2
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    #{} is used for inserting Ruby inside strings.

    You could do:

    Code:
    @person = eval(modelName).new
    Make sure you know where the modelName is coming from for security reasons.

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    Oklahoma
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Luke Redpath
    #{} is used for inserting Ruby inside strings.

    You could do:

    Code:
    @person = eval(modelName).new
    Make sure you know where the modelName is coming from for security reasons.
    Or the safer

    Code:
    @person = Kernel.const_get(modelName).new

  4. #4
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe the cleaner but less dynamic:

    Code:
    model = Person
    person = model.new
    will suffice?

  5. #5
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think the OP was expecting the class name to be a string.

  6. #6
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    Oklahoma
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Luke Redpath
    I think the OP was expecting the class name to be a string.
    I'm not sure who you are addressing, but in the case of the code I posted above const_get takes a string. So it works as well as the eval version, just without the dangers of running eval

  7. #7
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Sgarissta
    I'm not sure who you are addressing, but in the case of the code I posted above const_get takes a string. So it works as well as the eval version, just without the dangers of running eval
    I was referring to fenrir

  8. #8
    Mal Reynolds Mandibal's Avatar
    Join Date
    Aug 2003
    Location
    Columbus
    Posts
    718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Heres a question:

    Would the prefered way be to use Kernel.const_get or Object.const_get. I've just used Object.const_get for dynamic model creation. Would there be an advantage to using Kernel instead or does it really not matter and it comes down to personal preference?
    Erh

  9. #9
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    Oklahoma
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mandibal
    Heres a question:

    Would the prefered way be to use Kernel.const_get or Object.const_get. I've just used Object.const_get for dynamic model creation. Would there be an advantage to using Kernel instead or does it really not matter and it comes down to personal preference?
    It's the same method. I think the Object one inherits from the Kernel version, but I can't remember, maybe not. But there isn't any difference between the two. In fact is you use the Kernel version, you can avoid Kernel. and just say const_get, as Kernel is always mixed into the current scope.

  10. #10
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nope. It's "class/module" method of Kernel, so you have to use Kernel.const_get.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •