Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How can I do this without eval...

Reply
Thread Tools

How can I do this without eval...

 
 
J2M
Guest
Posts: n/a
 
      09-01-2006
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
 
Reply With Quote
 
 
 
 
Eero Saynatkari
Guest
Posts: n/a
 
      09-01-2006
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/.

 
Reply With Quote
 
 
 
 
Jan Svitok
Guest
Posts: n/a
 
      09-01-2006
On 9/2/06, J2M <(E-Mail Removed)> 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.

 
Reply With Quote
 
J2M
Guest
Posts: n/a
 
      09-02-2006

Jan Svitok wrote:

> model = const_get(model_name.to_s.capitalize)


Excellent, thank you.


 
Reply With Quote
 
Paul Duncan
Guest
Posts: n/a
 
      09-02-2006
--GyRA7555PLgSTuth
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

* J2M ((E-Mail Removed)) 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 <(E-Mail Removed)> 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+ecczdlT34LClWIRAnSXAJ9tlpxaaTBSuUugQDptEX UtoKK79QCfTrkf
09ZARqbi2tjeZ7Eur94AtoI=
=3rEl
-----END PGP SIGNATURE-----

--GyRA7555PLgSTuth--

 
Reply With Quote
 
Ezra Zygmuntowicz
Guest
Posts: n/a
 
      09-02-2006

On Sep 2, 2006, at 1:19 PM, Paul Duncan wrote:

> * J2M ((E-Mail Removed)) 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 <(E-Mail Removed)> pabs in #ruby-lang (OPN IRC)
> http://www.pablotron.org/ OpenPGP Key ID: 0x82C29562



 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Dimdows Command-Line: Can't Live With It, Can't Live Without It Lawrence D'Oliveiro NZ Computing 15 08-17-2009 11:59 AM
Can't connect to local machine with anything except IE Other browsers can't connect without password Otis Mukinfus ASP .Net 3 07-08-2005 03:54 AM
How to store session ID without cookies and without "Cookieless=True" Paul W ASP .Net 4 06-02-2005 09:34 AM
open a new window without javascript and without hyperlink Dan ASP .Net 8 11-24-2004 08:10 AM



Advertisments