"Ruby" (External Evaluation System)
Details
- Ruby Version 2.0 and higher is supported.
- To configure Ruby for use in the Wolfram Language, follow the instructions from the Configure Ruby for ExternalEvaluate workflow.
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.
- Possible settings for "type" in ExternalEvaluate["Ruby""type",code] include:
-
"Expression" attempt to convert to a Wolfram Language expression "String" give the raw string output by the external evaluator "ExternalObject" return the result as ExternalObject
Data Types
- The following Ruby built-in types are supported:
-
Array List array of values BigDecimal Real arbitrary-precision real number Bignum Integer arbitrary-sized integer boolean TrueFalse Boolean values Complex Complex complex number Fixnum Integer machine-size number Float Real real-valued number Hash Association associative array nil Null null value Rational Rational rational number String String sequence of character values
Supported External Operations
- ExternalOperation["Eval","code"] represents an external evaluation of "code".
- ExternalOperation["Eval","code",assoc] represents an external evaluation of "code" with parameters given by assoc.
- ExternalOperation["Call",func,arg1,arg2,…] calls the function func with the given arguments arg1, arg2, ….
- ExternalOperation["GetAttribute",obj,"attr"] gets the attribute "attr" of obj.
- ExternalOperation["SetAttribute",obj,"attr",val] sets the attribute "attr" of obj to the given value val.
- ExternalOperation["Cast",obj,"type"]casts obj to the given "type".
Examples
open allclose allBasic Examples (3)
Evaluate 2+2 in Ruby and return the result:
Type > and select Ruby from the drop-down menu to get a code cell that uses ExternalEvaluate to evaluate:
Scope (28)
Concatenate strings in Ruby and return the result:
Hashes in Ruby are returned as associations:
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)
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)
"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:
"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:
External Operations (8)
"Eval" (1)
Run an ExternalOperation that represents arbitrary code evaluation in Python:
"Call" (3)
Define an ExternalOperation that creates a function in Ruby:
Call the function by running the ExternalOperation "Call":
Run the operation using ExternalEvaluate:
Any argument of the "Call" operation can be an ExternalOperation:
Arguments can also be passed directly in ExternalEvaluate by doing the following:
The result is equivalent to running the following Ruby code:
Create an ExternalFunction for a Ruby function:
Call the function by running the operation "Call":
The same result can be achieved by doing the following:
Create an ExternalObject for a function in Ruby:
Call the function by running the operation "Call":
The same result can be achieved by doing the following:
Or by using ExternalObject subvalues:
"GetAttribute" (2)
Start a Ruby session to work with dates:
Return an ExternalObject for a Date object:
Extract the year attribute by using "GetAttribute":
The result is equivalent to running the following Ruby code:
Create an ExternalObject that represents a date:
Use ExternalOperation to get the current year:
For most evaluators, "GetAttribute" is the default operation, and ExternalOperation can be omitted:
"SetAttribute" (1)
"Cast" (1)
Create an ExternalObject that represents the current date:
Use "Expression" to return the object as a Wolfram Language expression:
The Cast operation can also run in ExternalObject subvalues:
The symbol Expression is a shortcut for the same:
Return the object as a string:
The symbol String is a shortcut for the same:
Return the object as an ExternalObject:
The symbol ExternalObject is a shortcut for the same:
The same can be achieved by using "ReturnType" in ExternalEvaluate: