[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Method for turning strings into code

Steve Litt

12/30/2005 3:37:00 PM

Hi all,

What is the Ruby technique for turning strings into executable code?

SteveT

Steve Litt
http://www.troublesh...
slitt@troubleshooters.com


11 Answers

Johannes Friestad

12/30/2005 3:48:00 PM

0

eval

>eval("3+3")
6

jf
On 12/30/05, Steve Litt <slitt@earthlink.net> wrote:
> Hi all,
>
> What is the Ruby technique for turning strings into executable code?
>
> SteveT
>
> Steve Litt
> http://www.troublesh...
> slitt@troubleshooters.com
>
>


--
Johannes Friestad
johannes.friestad@gmail.com


james_b

12/30/2005 3:51:00 PM

0

Steve Litt wrote:
> Hi all,
>
> What is the Ruby technique for turning strings into executable code?

There's eval:

eval( 'class Foo;def x; puts "x"; end;end' )

Foo.new.x

And its variants (instance_eval, class_eval)


James
--

http://www.ru... - Ruby Help & Documentation
http://www.artima.c... - Ruby Code & Style: Writers wanted
http://www.rub... - The Ruby Store for Ruby Stuff
http://www.jame... - Playing with Better Toys
http://www.30seco... - Building Better Tools


Tim Hunter

12/30/2005 3:51:00 PM

0

Steve Litt wrote:
> Hi all,
>
> What is the Ruby technique for turning strings into executable code?
>
Via one of the various forms of eval. However, using eval on arbitrary
strings is insecure, so generally it's considered bad form. There's even
a slogan: "Eval is evil." The preferred technique is to build a solution
using Ruby's dynamic programming facilities.

Steve Litt

12/30/2005 4:07:00 PM

0

On Friday 30 December 2005 10:51 am, James Britt wrote:
> Steve Litt wrote:
> > Hi all,
> >
> > What is the Ruby technique for turning strings into executable code?
>
> There's eval:
>
> eval( 'class Foo;def x; puts "x"; end;end' )
>
> Foo.new.x
>
> And its variants (instance_eval, class_eval)
>
>
> James

Thanks James, and you to Johannes.

SteveT

Steve Litt
http://www.troublesh...
slitt@troubleshooters.com


Erik Veenstra

12/30/2005 4:10:00 PM

0

> > What is the Ruby technique for turning strings into
> > executable code?
>
> Via one of the various forms of eval.

Uh, well, that's not the right answer. Eval evaluates
(=executes) a string. It doesn't turn a string into executable
code (=compile).

gegroet,
Erik V. - http://www.erikve...

Christian Neukirchen

12/31/2005 1:32:00 PM

0

Erik Veenstra <pan@erikveen.dds.nl> writes:

>> > What is the Ruby technique for turning strings into
>> > executable code?
>>
>> Via one of the various forms of eval.
>
> Uh, well, that's not the right answer. Eval evaluates
> (=executes) a string. It doesn't turn a string into executable
> code (=compile).

Wrap it in a lambda, then.

> gegroet,
> Erik V. - http://www.erikve...
--
Christian Neukirchen <chneukirchen@gmail.com> http://chneuk...


Steve Litt

12/31/2005 1:37:00 PM

0

On Saturday 31 December 2005 06:07 am, Erik Veenstra wrote:
> > > What is the Ruby technique for turning strings into
> > > executable code?
> >
> > Via one of the various forms of eval.
>
> Uh, well, that's not the right answer. Eval evaluates
> (=executes) a string. It doesn't turn a string into executable
> code (=compile).

When I first posed the question, what I really meant was turn it into Ruby
code (like eval does). Sorry for the confusion.

While we're on the subject, do you know of a way to turn a Ruby program into
an executable binary?

Thanks

SteveT

Steve Litt
http://www.troublesh...
slitt@troubleshooters.com


Steve Litt

12/31/2005 1:37:00 PM

0

On Saturday 31 December 2005 06:07 am, Timothy Hunter wrote:
> Steve Litt wrote:
> > Hi all,
> >
> > What is the Ruby technique for turning strings into executable code?
>
> Via one of the various forms of eval. However, using eval on arbitrary
> strings is insecure, so generally it's considered bad form. There's even
> a slogan: "Eval is evil." The preferred technique is to build a solution
> using Ruby's dynamic programming facilities.

Where do I find out more about Ruby's dynamic programming facilities?

SteveT

Steve Litt
http://www.troublesh...
slitt@troubleshooters.com


Erik Veenstra

12/31/2005 2:22:00 PM

0

> > While we're on the subject, do you know of a way to turn a
> > Ruby program into an executable binary?

If you want to distribute your Ruby application as a single
executable, including the Ruby interpreter and libraries, use
RubyScript2Exe:

http://www.erikve...rubyscript2exe/...

(Yes, it's one of my own projects... But you asked me...)

gegroet,
Erik V. - http://www.erikve...

Tim Hunter

12/31/2005 2:24:00 PM

0

Steve Litt wrote:
> Where do I find out more about Ruby's dynamic programming facilities?

The Pickaxe would be a good start. Also Hal's _The Ruby Way_.