Method for turning strings into code

Discussion in 'Ruby' started by Steve Litt, Dec 30, 2005.

  1. Steve Litt

    Steve Litt Guest

    Steve Litt, Dec 30, 2005
    #1
    1. Advertising

  2. eval

    >eval("3+3")

    6

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



    --
    Johannes Friestad
    Johannes Friestad, Dec 30, 2005
    #2
    1. Advertising

  3. Steve Litt

    James Britt Guest

    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
    James Britt, Dec 30, 2005
    #3
  4. Steve Litt

    Steve Litt Guest

    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.troubleshooters.com
    Steve Litt, Dec 30, 2005
    #4
  5. > > 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/
    Erik Veenstra, Dec 30, 2005
    #5
  6. Steve Litt

    Steve Litt Guest

    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
    Steve Litt, Dec 31, 2005
    #6
  7. Steve Litt

    Steve Litt Guest

    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
    Steve Litt, Dec 31, 2005
    #7
  8. > > 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/
    Erik Veenstra, Dec 31, 2005
    #8
  9. Steve Litt

    James Britt Guest

    Christian Neukirchen wrote:
    > Erik Veenstra <> 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.


    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
    James Britt, Dec 31, 2005
    #9
    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. Freddy

    Turning Strings into Functions

    Freddy, Sep 26, 2003, in forum: Python
    Replies:
    3
    Views:
    363
    Freddy
    Sep 26, 2003
  2. Replies:
    4
    Views:
    322
  3. dylpkls91

    Help turning code into a module

    dylpkls91, Dec 21, 2005, in forum: Python
    Replies:
    0
    Views:
    271
    dylpkls91
    Dec 21, 2005
  4. Robert
    Replies:
    0
    Views:
    508
    Robert
    Feb 12, 2006
  5. Robert
    Replies:
    0
    Views:
    123
    Robert
    Feb 12, 2006
Loading...

Share This Page