How can I do this without eval...

Discussion in 'Ruby' started by J2M, Sep 2, 2006.

  1. J2M

    J2M Guest

    def load_default_attributes(model_name, options = {})
    fixture = options[:fixture] || :default
    attributes = temp_attributes = model = {}
    instance_eval <<-FIN
    temp_attributes = #{model_name.to_s.pluralize}("#{fixture}")
    model = #{model_name.to_s.capitalize}
    FIN
    J2M, Sep 2, 2006
    #1
    1. Advertising

  2. James Mccarthy wrote:
    > def load_default_attributes(model_name, options = {})
    > fixture = options[:fixture] || :default
    > attributes = temp_attributes = model = {}
    > instance_eval <<-FIN
    > temp_attributes = #{model_name.to_s.pluralize}("#{fixture}")


    send model_name.to_s.pluralize, fixture

    > model = #{model_name.to_s.capitalize}


    self.class.const_get model_name.to_s.capitalize

    > FIN
    > .
    > .
    > .
    > end
    >
    > I know there must be a neater way to get hold of a class (or in this
    > case a Rails model) and with the temp_attributes to get hold of a
    > fixture, can anybody shed some metaprogramming light?


    The above should work in principle; however, take a
    look at ri Object#send and Module#const_get in case
    you run into any problems.

    (Disclaimer: respondent is not proficient with Rails.)

    > Thanks.



    --
    Posted via http://www.ruby-forum.com/.
    Eero Saynatkari, Sep 2, 2006
    #2
    1. Advertising

  3. J2M

    Jan Svitok Guest

    On 9/2/06, J2M <> wrote:
    > def load_default_attributes(model_name, options = {})
    > fixture = options[:fixture] || :default
    > attributes = temp_attributes = model = {}
    > instance_eval <<-FIN
    > temp_attributes = #{model_name.to_s.pluralize}("#{fixture}")
    > model = #{model_name.to_s.capitalize}
    > FIN


    Hi,

    model = const_get(model_name.to_s.capitalize)

    Classes are constants. So you can get them by name (string or symbol)
    with Module#const_get

    temp_attributes... I don't know what fixtures are, I'll just say that you can
    - call a method with Object#send(name, arg1, arg2) or send(name, *args)
    - get/set instance variable with
    instance_variable_get/instance_variable_set (don't forget the @!
    my_object.instance_variable_get('@name') )
    - class variables with Module#class_variable_get/.._set

    and generally, look into Module, Class, Object and Kernel for other
    useful methods ;-)

    finally http://www.rubycentral.com/book/ospace.html can be helpful.
    Jan Svitok, Sep 2, 2006
    #3
  4. J2M

    J2M Guest

    Jan Svitok wrote:

    > model = const_get(model_name.to_s.capitalize)


    Excellent, thank you.
    J2M, Sep 2, 2006
    #4
  5. J2M

    Paul Duncan Guest

    --GyRA7555PLgSTuth
    Content-Type: text/plain; charset=us-ascii
    Content-Disposition: inline
    Content-Transfer-Encoding: quoted-printable

    * J2M () wrote:
    >=20
    > Jan Svitok wrote:
    >=20
    > > model =3D const_get(model_name.to_s.capitalize)


    This might be more appropriate:

    model_class =3D const_get(model_name.to_s.classify)

    Classify will take care of things that capitalize misses, like
    underscores and extraneous plurals. Take a look at the following
    output:

    # test strings and test methods
    strings =3D %w{blog_post layers magazine_subscribers}
    methods =3D %w{capitalize classify}.map { |m| m.intern }

    # iterate over test strings and handle each one
    puts strings.map { |str|
    "'#{str}'\n" << methods.map { |meth|=20
    " #{meth}:\t'#{str.send(meth)}'"=20
    }.join("\n")
    }

    Produces the following:

    'blog_post'
    capitalize: 'Blog_post'
    classify: 'BlogPost'
    'layers'
    capitalize: 'Layers'
    classify: 'Layer'
    'magazine_subscribers'
    capitalize: 'Magazine_subscribers'
    classify: 'MagazineSubscriber'

    Hope that helps...

    --=20
    Paul Duncan <> pabs in #ruby-lang (OPN IRC)
    http://www.pablotron.org/ OpenPGP Key ID: 0x82C29562

    --GyRA7555PLgSTuth
    Content-Type: application/pgp-signature; name="signature.asc"
    Content-Description: Digital signature
    Content-Disposition: inline

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.0 (GNU/Linux)

    iD8DBQFE+ecczdlT34LClWIRAnSXAJ9tlpxaaTBSuUugQDptEXUtoKK79QCfTrkf
    09ZARqbi2tjeZ7Eur94AtoI=
    =3rEl
    -----END PGP SIGNATURE-----

    --GyRA7555PLgSTuth--
    Paul Duncan, Sep 2, 2006
    #5
  6. On Sep 2, 2006, at 1:19 PM, Paul Duncan wrote:

    > * J2M () wrote:
    >>
    >> Jan Svitok wrote:
    >>
    >>> model = const_get(model_name.to_s.capitalize)

    >
    > This might be more appropriate:
    >
    > model_class = const_get(model_name.to_s.classify)


    Also if you are already within rails you may as well use #constantize

    model_class = model_name.to_s.classify.constantize

    -Ezra

    >
    > Classify will take care of things that capitalize misses, like
    > underscores and extraneous plurals. Take a look at the following
    > output:
    >
    > # test strings and test methods
    > strings = %w{blog_post layers magazine_subscribers}
    > methods = %w{capitalize classify}.map { |m| m.intern }
    >
    > # iterate over test strings and handle each one
    > puts strings.map { |str|
    > "'#{str}'\n" << methods.map { |meth|
    > " #{meth}:\t'#{str.send(meth)}'"
    > }.join("\n")
    > }
    >
    > Produces the following:
    >
    > 'blog_post'
    > capitalize: 'Blog_post'
    > classify: 'BlogPost'
    > 'layers'
    > capitalize: 'Layers'
    > classify: 'Layer'
    > 'magazine_subscribers'
    > capitalize: 'Magazine_subscribers'
    > classify: 'MagazineSubscriber'
    >
    > Hope that helps...
    >
    > --
    > Paul Duncan <> pabs in #ruby-lang (OPN IRC)
    > http://www.pablotron.org/ OpenPGP Key ID: 0x82C29562
    Ezra Zygmuntowicz, Sep 2, 2006
    #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. Eric Newton
    Replies:
    3
    Views:
    9,392
    Brock Allen
    Apr 4, 2005
  2. DataBinder.Eval and Eval.

    , Jun 16, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    542
    Karl Seguin [MVP]
    Jun 16, 2006
  3. Alex van der Spek

    eval('07') works, eval('08') fails, why?

    Alex van der Spek, Jan 8, 2009, in forum: Python
    Replies:
    6
    Views:
    1,435
    Bruno Desthuilliers
    Jan 8, 2009
  4. Liang Wang
    Replies:
    8
    Views:
    129
    Ben Morrow
    Feb 2, 2008
  5. Marc Girod

    to eval or not to eval?

    Marc Girod, Apr 19, 2011, in forum: Perl Misc
    Replies:
    2
    Views:
    155
    Marc Girod
    Apr 19, 2011
Loading...

Share This Page