Runtime interpretation

I want to know how to interpret something from a string, so as to be used
directly by the interpreter at runtime. A simplified example:

$> ruby myprogram.rb
Enter object information:> Array.new(4)
object created:> [nil, nil, nil, nil]
Enter object information:> Hash.new()
object created:> {}
Enter object information:> MyObject.new(2,4,6)
etc., etc.

I know that, as an interpreted language, Ruby can load new classes, function
definitions, etc. at runtime. But is the above possible? If the object
creation was limited to Arrays and Hash’s, it would be simple enough just to
parse the string and go from there… but that severly limits the
funcationality with such specificity. I was hoping to be able to directly
interpret the information in the input string. Is this possible?

···

MSN 8 with e-mail virus protection service: 2 months FREE*
http://join.msn.com/?page=features/virus

[ensemble] ~ $ ri eval
----------------------------------------------------------- Kernel::eval
eval( aString [, aBinding [file [line]]]) → anObject

···
 Evaluates the Ruby expression(s) in aString. If aBinding is given,
 the evaluation is performed in its context. The binding may be a
 Binding object or a Proc object. If the optional file and line
 parameters are present, they will be used when reporting syntax
 errors.
    def getBinding(str)
      return binding
    end
    str = "hello"
    eval "str + ' Fred'"                      #=> "hello Fred"
    eval "str + ' Fred'", getBinding("bye")   #=> "bye Fred"

Cheers,
Sam

Quoteing orion2480@hotmail.com, on Tue, Jun 24, 2003 at 02:21:45AM +0900:

I want to know how to interpret something from a string, so as to be used
directly by the interpreter at runtime. A simplified example:

$> ruby myprogram.rb
Enter object information:> Array.new(4)
object created:> [nil, nil, nil, nil]
Enter object information:> Hash.new()
object created:> {}
Enter object information:> MyObject.new(2,4,6)
etc., etc.

I know that, as an interpreted language, Ruby can load new classes,
function definitions, etc. at runtime. But is the above possible? If the
object creation was limited to Arrays and Hash’s, it would be simple enough
just to parse the string and go from there… but that severly limits the
funcationality with such specificity. I was hoping to be able to directly
interpret the information in the input string. Is this possible?


MSN 8 with e-mail virus protection service: 2 months FREE*
http://join.msn.com/?page=features/virus

You mean this?

batsman@tux-chan:/tmp/ruby-1.8.0$ expand -t 2 /tmp/az.rb

require ‘readline’

loop do
line = Readline.readline("Enter object information:> ", true)
obj = eval line
puts “object created:> #{obj.inspect}”
end

batsman@tux-chan:/tmp/ruby-1.8.0$ ruby /tmp/az.rb
Enter object information:> Array.new(4)
object created:> [nil, nil, nil, nil]
Enter object information:> Hash.new()
object created:> {}

···

On Tue, Jun 24, 2003 at 02:21:45AM +0900, Orion Hunter wrote:

I want to know how to interpret something from a string, so as to be used
directly by the interpreter at runtime. A simplified example:

$> ruby myprogram.rb
Enter object information:> Array.new(4)
object created:> [nil, nil, nil, nil]
Enter object information:> Hash.new()
object created:> {}
Enter object information:> MyObject.new(2,4,6)
etc., etc.


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Software is like sex; it’s better when it’s free.
– Linus Torvalds