Forum: Ruby Method for turning strings into code

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Steve Litt (Guest)
on 2005-12-30 16:40
(Received via mailing list)
Hi all,

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

SteveT

Steve Litt
http://www.troubleshooters.com
slitt@troubleshooters.com
Johannes Friestad (Guest)
on 2005-12-30 16:49
(Received via mailing list)
eval

>eval("3+3")
6

jf
James Britt (Guest)
on 2005-12-30 16:52
(Received via mailing list)
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.ruby-doc.org       - Ruby Help & Documentation
http://www.artima.com/rubycs/ - Ruby Code & Style: Writers wanted
http://www.rubystuff.com      - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com     - Playing with Better Toys
http://www.30secondrule.com   - Building Better Tools
Steve Litt (Guest)
on 2005-12-30 17:08
(Received via mailing list)
On Friday 30 December 2005 10:51 am, James Britt wrote:
>
> And its variants (instance_eval, class_eval)
>
>
> James

Thanks James, and you to Johannes.

SteveT

Steve Litt
http://www.troubleshooters.com
slitt@troubleshooters.com
31199d2640de571fcd470c6d26124fa4?d=identicon&s=25 Timothy H. (tim-hunter)
on 2005-12-31 12:36
(Received via mailing list)
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 (Guest)
on 2005-12-31 14:40
(Received via mailing list)
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.troubleshooters.com
slitt@troubleshooters.com
31199d2640de571fcd470c6d26124fa4?d=identicon&s=25 Timothy H. (tim-hunter)
on 2005-12-31 15:28
(Received via mailing list)
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_.
Erik Veenstra (Guest)
on 2006-01-01 21:32
(Received via mailing list)
> > 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.erikveen.dds.nl/
Christian Neukirchen (Guest)
on 2006-01-01 21:32
(Received via mailing list)
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.
Steve Litt (Guest)
on 2006-01-01 21:32
(Received via mailing list)
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.troubleshooters.com
slitt@troubleshooters.com
Erik Veenstra (Guest)
on 2006-01-01 21:32
(Received via mailing list)
> > 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.erikveen.dds.nl/rubyscript2exe/index.html

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

gegroet,
Erik V. - http://www.erikveen.dds.nl/
James Britt (Guest)
on 2006-01-01 21:32
(Received via mailing list)
Christian Neukirchen wrote:
>>code (=compile).
>
>
> Wrap it in a lambda, then.

If I have a string in a file, would we say it was executable code?  Or
does loading it into the Ruby interpreter make it executable code?


What are the differences among

   require 'my-file-of-strings'


   load 'my-file-of-strings'

and

   eval( IO.read( 'my-file-of-strings' ) )



James
--

http://www.ruby-doc.org       - Ruby Help & Documentation
http://www.artima.com/rubycs/ - Ruby Code & Style: Writers wanted
http://www.rubystuff.com      - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com     - Playing with Better Toys
http://www.30secondrule.com   - Building Better Tools
This topic is locked and can not be replied to.