"Ruby" (External Evaluation System)

Details

ExternalEvaluate Usage

  • ExternalEvaluate["Ruby",code] executes the code string in a Ruby REPL and returns the results as a Wolfram Language expression.
  • ExternalEvaluate["Ruby""String",code] executes the code string in a Ruby REPL and returns the output as a Wolfram Language string.

Data Types

  • The following Ruby built-in types are supported:
  • ArrayListarray of values
    BigDecimalRealarbitrary-precision real number
    BignumIntegerarbitrary-sized integer
    booleanTrue|FalseBoolean values
    ComplexComplexcomplex number
    FixnumIntegermachine-size number
    FloatRealreal-valued number
    HashAssociationassociative array
    nilNullnull value
    RationalRationalrational number
    StringStringsequence of character values

Usage Notes

  • String templates (<**>) can be used to evaluate and insert Wolfram Language expressions into the Ruby code string.

Examples

open allclose all

Basic Examples  (3)

Evaluate 2+2 in Ruby and return the result:

Arrays are returned as lists:

Type > and select Ruby from the drop-down menu to get a code cell that uses ExternalEvaluate to evaluate:

[1,2,3,4].map { |n| n * 2 }

Scope  (20)

Start a Ruby session:

Concatenate strings in Ruby and return the result:

Hashes in Ruby are returned as associations:

Close the session:

Session Options  (9)

"ReturnType"  (3)

For the Ruby evaluation system, the default return type is "Expression":

Numbers, strings, lists and associations are automatically imported for the "Expression" return type:

The return type of "String" returns a string of the result by calling the Ruby method to_s:

"Version"  (1)

You can use "Version" to make sure only a particular Ruby version is used:

You can specify a minor or patch version:

"Evaluator"  (1)

Evaluate Ruby code using a specified "Evaluator":

"SessionProlog"  (1)

Use "SessionProlog" to perform a side effect at the start of a session:

"SessionEpilog"  (1)

Use "SessionEpilog" to perform a side effect at the end of a session:

"Prolog"  (1)

Use "Prolog" to perform a side effect before every evaluation:

"Epilog"  (1)

Use "Epilog" to perform a side effect after every evaluation:

Command Options  (10)

"Command"  (3)

When only a string of Ruby code is provided, the command is directly executed:

The above is equivalent to writing the command using this form:

Use a File wrapper to run the code in a file:

The above is equivalent to writing the command using this form:

Put code in a CloudObject:

Evaluate directly from the cloud:

The above is equivalent to writing the command using this form:

"ReturnType"  (1)

By default, the command is executed using the "ReturnType" specified during the session creation:

Specifying a "ReturnType" in the command overrides the "ReturnType" for the session:

"Arguments"  (2)

Use "Arguments" to call a Ruby function with arguments:

When a non-list argument is provided, a single argument is passed to the function:

If you need to pass a list as the first argument, you must wrap it with an extra list explicitly:

You can define a function inside "Command" and directly call it with "Arguments":

The same result can be achieved by using a Rule:

You can also pass arguments by creating an ExternalFunction:

"Constants"  (1)

Use "Constants" to permanently set global variables before the command runs:

"TemplateArguments"  (3)

When running a command, you can inline a TemplateExpression:

You can explicitly fill TemplateSlot using "TemplateArguments":

When a non-list argument is provided, a single template argument is passed to the template:

If you need to pass a list as the first argument, you must wrap it with an extra list explicitly:

You can name template slots and use an Association to pass named arguments to the template:

Applications  (1)

Define a Wolfram Language function that calls a custom Ruby function:

Close the session: