How do you evaluate text as code in Ruby?

How do you evaluate text as code in Ruby?
I’m sure there’s a technical term for this…

Thanks
Chris

I’ve just found out, you use “eval”, suprisingly enough

eval(“code”)

Hi –

On Tue, 1 Aug 2006, Chris wrote:

How do you evaluate text as code in Ruby?
I’m sure there’s a technical term for this…

There are two steps:

  1. Use eval(string)
  2. Examine your program, find out what led you to use eval,
    and fix it :slight_smile:

David


http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com => D[avid ]A[. ]B[lack’s][ Web]log
http://www.manning.com/black => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.

On Aug 1, 2006, at 6:20 AM, Chris wrote:

How do you evaluate text as code in Ruby?
I’m sure there’s a technical term for this…

Thanks
Chris

Be sure to check out eval’s cousins instance_eval, class_eval and
module_eval.
Instance eval is particularly cool because it can take a block too,
eliminating the ‘code in a string’ problem.

There’s also implementations of instance_exec which is like
instance_eval but with arbitrary argument passing.

While you’re into the meta-programming world, define_method,
remove_method and alias are pretty darn cool too.

It’s a wide wide ruby world :slight_smile:
-Mat

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs